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)
精彩评论