我是一个Rails noob,并有一个问题。我有一个是通过这个笼统的概念举办的馈送聚合:belongs_to与多个模型
饲料类(书籍,电子产品等) 进料部位科(主页,书页等) 饲料(饲料本身) 提要条目
所以:
class Category < ActiveRecord::Base
has_many :feeds
has_many :feed_entries, :through => :feeds, :limit => 5
validates_presence_of :name
attr_accessible :name, :id
end
class Section < ActiveRecord::Base
has_many :feeds
has_many :feed_entries, :through => :feeds, :limit => 5
attr_accessible :name, :id
end
class Feed < ActiveRecord::Base
belongs_to :categories
belongs_to :sections
has_many :feed_entries
validates_presence_of :name, :feed_url
attr_accessible :name, :feed_url, :category_id, :section_id
end
class FeedEntry < ActiveRecord::Base
belongs_to :feed
belongs_to :category
belongs_to :section
validates_presence_of :title, :url
end
有意义吗?现在,在我的索引页面中,我想基本上说...如果您处于分类图书的主页部分,请提供按Feed分组的Feed输入...
在我的控制器中:
def index
@section = Section.find_by_name("Home Page")
@books = Category.find_by_name("Books")
end
笔者认为:
<%= render :partial => 'feed_list',
:locals => {:feed_group => @books.feeds}
-%>
这部分将吐出的饲料中的@books集合中的每个Feed条目中的标记。现在我需要做的就是以某种方式与@section结合@books ...
我尝试这样做:
<%= render :partial => 'feed_list',
:locals => {:feed_group => @books.feeds(:section_id => @section.id)}
-%>
但它不是由部分ID限制。我已通过在控制台中使用相同的代码确认了部分ID ...
有意义吗?有什么建议?
谢谢!
我有以下的麻烦。 Category和Section有什么区别?只需在控制器中添加一个查找,例如'@feeds = Feed.find:all,:conditions => [“section_id =?and category_id =?”,@ section.id,@ page.id]'做你自己寻找? – Awgy 2010-06-15 02:53:43