2012-11-21 94 views
2

我有以下模型獲取所有嵌套資源記錄

User 
has_many :threads 
.... 

Thread 
belongs_to :user 
has_many :posts 
....  

Post 
belongs_to :thread 
.... 

要用戶我只是User.find(params[:id]).threads 可我怎麼能得到用戶的所有帖子得到的所有主題?

回答

4
 

class User 
    has_many :threads 
    has_many :posts, :through => :threads 
end 
 

User.find(PARAMS [:編號])。除非我是今天早上岱恩 - bramaged職位

應該做的伎倆。

2
User 
has_many :threads 
has_many :posts, :through => :threads 
.... 

Thread 
belongs_to :user 
has_many :posts 
....  

Post 
belongs_to :thread 

那麼你應該能夠做到:

user = User.first 
user.posts