I have following structure:
class Delivery < ActiveRecord::Base
belongs_to :shop
end
and
class Shop < ActiveRecord::Base
has_many :delivery
end
and in view
<% @shops.each do |shop| %>
<% 开发者_开发百科@deliveries.each do |dlv| %>
<div><%= dlv.shop.type %></div>
<%end%>
<% end %>
getting an error
undefined method `type' for "#":Shop
I am printing data from table Shops and for each item from this table I want to display items from tabe "Deliveries". I though the associations are right, but if I am getting the error above, I am not so sure already...
I would like to ask you about help, what could be wrong. Thank you in advance.
<% @shops.each do |shop| %>
<% shop.delivery.each do |dlv| %>
<div><%= dlv.type %></div>
<%end%>
<% end %>
The point of associations is that you can access it from an instance of the model. Note, that has_many
associations should be pluralized.
精彩评论