how to query in view ruby on rails -
i have in controller
@ads = ad.all(:joins => 'left join states
on ads.state_id = states.id')
but have problem query field of states table. idea?
<% @ads.each |ad| %> <tr> <td><%= ad.title %></td> <- title ad field.no problem <td><%= ad.name %></td> <- name states field.problem @ here </tr> <% end %>
i don't think work unless have associations set up. unless performance concern, may want use association without joins
ad.rb
class ad < activerecord::base belongs_to :state end
state.rb
class state < activerecord::base has_many :ads end
controller
@ads = ad.all
view
<% @ads.each |ad| %> <tr> <td><%= ad.title %></td> <td> <%= ad.state.name %> </td> </tr> <% end %>
Comments
Post a Comment