如何通过关系访问 has_many 中的属性?
我如何在这里访问商店名称?@deal 实例可以完美访问商品名称,但是当我尝试访问商店名称时,它给了我
no method error 'stores'
如何获取商店实例来访问商店名称?这是代码。提前谢谢您
控制器
def show
unless session[:city_id].nil? || session[:city_id].blank?
@city = City.find(session[:city_id])
@[email protected]
@[email protected]
end
视图/展示
<% @deal.each do |deal| %>
<%=deal.item_name %>
<%end%>
<%[email protected]_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
How do i access store name here?@deal instance can access perfectly item name,but when i try to access store name it gives me
no method error 'stores'
How do i get store instance to access store name?Here is the code.Thank you in advance
controller
def show
unless session[:city_id].nil? || session[:city_id].blank?
@city = City.find(session[:city_id])
@[email protected]
@[email protected]
end
view/show
<% @deal.each do |deal| %>
<%=deal.item_name %>
<%end%>
<%[email protected]_name %>
models
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@deal 很可能是一系列交易。 @city.deals 意味着您可以在每个城市有多个交易。 时,这会导致问题:
当您尝试执行以下操作
@deal is most likely an array of deals. @city.deals implies you could have multiple deals per city. This wil cause a problem when you try to do
try something like: