0

我的模型是:

class City < ActiveRecord::Base
  belongs_to :region
  has_many  :activities
  has_many  :restaurants
end


class Activity < ActiveRecord::Base
    belongs_to :city
end

在城市索引页面上,我想根据每个循环中的布尔值显示所有城市的活动和餐厅。

我的城市管理员:

def index 
@region = Region.find(params[:region_id])
@cities = @region.cities

@activities = @cities.activities.find_all_by_homepage_city(true)
@restaurants = @city.restaurants.find_all_by_homepage_city(true)
@activities_restaurants = @activities + @restaurants
end

查看城市索引页面

- @activities_restaurants.each do |b|
              %li 
                =link_to b.name, polymorphic_path([@region, @city, b])

我收到错误消息“未定义的方法‘活动’”我做错了什么?

4

2 回答 2

1

此行导致错误:

@activities = @cities.activities.find_all_by_homepage_city(true)

您不能调用@cities.activities,而是执行以下操作:

通过关联添加 has_many 到 Region

class Region < ActiveRecord::Base
  has_many :activities, :through => :cities
end

然后你就可以打电话了:

@activities = @region.activities.find_all_by_homepage_city(true)
于 2012-08-13T21:27:15.157 回答
0

Is @city set somewhere else? Not seeing it in the code here.

I would try simplifying your code. If you reduce @activities_restaurants to just @activities or @restaurants, rather than both, does either work?

That'll get you started at least.

于 2012-08-13T21:16:04.597 回答