开发者

Rails 3 - problem with associations

开发者 https://www.devze.com 2023-03-17 02:31 出处:网络
I have following structure: class Delivery < ActiveRecord::Base belongs_to :shop end and class Shop < ActiveRecord::Base

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.

0

精彩评论

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