2010-06-15 46 views
1

我是一個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 ...

有意義嗎?有什麼建議?

謝謝!

+0

我有以下的麻煩。 Category和Section有什麼區別?只需在控制器中添加一個查找,例如'@feeds = Feed.find:all,:conditions => [「section_id =?and category_id =?」,@ section.id,@ page.id]'做你自己尋找? – Awgy 2010-06-15 02:53:43

回答

0

嘗試這樣:

:locals => {:feed_group => @books.feeds.all(:conditions => {:section_id => @section.id})} 
+0

從她的描述來看,這聽起來像是會起作用的。當你擁有控制器中所需的所有信息時,在視圖中擁有這種邏輯感覺不對,所以最好先在那裏建立@feeds。 – Awgy 2010-06-15 03:02:14

+0

同意。接得好。 – Jarrod 2010-06-15 03:06:35

+0

Ahoy!謝謝 - 完美的工作。 – panzhuli 2010-06-15 03:10:39

相關問題