开发者

Heroku STI Problem

This is my first time working with Single Table Inheritance, and it has been a great experience until pushing the app to Heroku. I have a User model (and table with a type column), a Hauler model that inherits from User class, and a Generator model that belongs to Hauler (and table with a hauler_id column). Here's the condensed code:

#hauler.rb model
class Hauler < User
  has_many :generators
end

#generator.rb model
class Generator < ActiveRecord::Base
 attr_accessible :name, :hauler_id
 belongs_to :hauler
end

#sessions_helper.rb
...
def hauler?
 current_user.type==Hauler
 if current_us开发者_如何学Goer.type==Hauler
  @generator = current_user.generators
 end
end

#home.html.erb
<% if hauler? %>
 <% unless @generator.nil? || @generator.emtpy? %>
  <ul>
  <% @generator.each do |g| %>
  <li><%= g.name %></li>
  <% end %>
  </ul>
 <% end %>
<% end %>

If a user with type=hauler is logged in, a list of their generators should be displayed on their home page. This works on my local machine, but not on Heroku. I've done some tinkering with the code and determined the problem to be in the sessions_helper file, but I'm not sure how to make it work. Running from Heroku, nothing contained within <% if hauler? %> is displayed. Just blank.


After another hour or so of tinkering, I have solved my own problem. Apparently Rails has a trouble accessing the type column for methods. Instead, the code should read something like this:

def hauler?
 current_user.class.name=="Hauler"
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜