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 回答