开发者

How do i access attributes in has_many :through relationship?

开发者 https://www.devze.com 2023-04-11 18:07 出处:网络
How do i access store name here?@deal instance can 开发者_如何学Pythonaccess perfectly item name,but when i try to access store name it gives me

How do i access store name here?@deal instance can 开发者_如何学Pythonaccess 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])
   @deal=@city.deals
   @store=@deal.stores
end

view/show

<% @deal.each do |deal| %>
<%=deal.item_name %>
<%end%>

<%=@store.store_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


@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

@deal=@city.deals
@store=@deal.stores

try something like:

@deals = @city.deals
@stores = @deals.collect(&:stores)
0

精彩评论

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