如何在此处访问商店名称?@deal实例可以完全访问商品名称,但是当我尝试访问商店名称时,它会给我
no method error 'stores'
如何获取商店实例以访问商店名称?这是代码。提前感谢
控制器
def show
unless session[:city_id].nil? || session[:city_id].blank?
@city = City.find(session[:city_id])
@deal=@city.deals
@store=@deal.stores
end
查看/显示
<% @deal.each do |deal| %>
<%=deal.item_name %>
<%end%>
<%=@store.store_name %>
模型
class Store < ActiveRecord::Base
has_many :deals ,:through =>:store_deals
has_many :store_deals
end
class Deal < ActiveRecord::Base
has_many :stores ,:through =>:store_deals
has_many :store_deals
end
class StoreDeal < ActiveRecord::Base
belongs_to :store
belongs_to :deal
end
答案 0 :(得分:2)
@deal很可能是一系列交易。 @ city.deals意味着每个城市可以有多笔交易。当你尝试
时,这会导致问题@deal=@city.deals
@store=@deal.stores
尝试类似:
@deals = @city.deals
@stores = @deals.collect(&:stores)