2

私はモデルに従う必要があります

User
has_many :threads
....

Thread
belongs_to :user
has_many :posts
....    

Post
belongs_to :thread
....

ユーザーからすべてのスレッドを取得するには、ユーザーUser.find( params[:id]).threads からすべての投稿を取得するにはどうすればよいですか?

4

2 に答える 2

4

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

User.find(params[:id]).posts

私が今朝大騒ぎしていない限り、トリックを行う必要があります。

于 2012-11-21T15:55:31.940 に答える
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
于 2012-11-21T16:03:43.467 に答える