我需要在索引视图中打印出所有 来自类别
class CategoryController < ApplicationController
def index
@city= City.find(session[:city_id])
@items=@city.items
end
end
索引视图
<%=@item.each do |item|%>
<%=item.item_name%>
<%end%>
模型
class City < ActiveRecord::Base
has_many :items
end
class Item < ActiveRecord::Base
belongs_to :city
belongs_to :category
end
class Category < ActiveRecord::Base
has_many :items
end
答案 0 :(得分:1)
控制器: 您应该可以执行以下操作:
@items = Item.find_by_city_and_category("city", "category")
或类似的东西:
@items = Item.where(:category => "category", :city => "city")
查看:
@items.each do |item|
item.name