0

私は、所属するユーザーに作成者エンティティを持っています。ユーザー has_many の投稿。ユーザーから Author エンティティに recent_posts を表示する方法を教えてください。

class User < ActiveRecord::Base
  has_many :posts, :foreign_key => "author_id"
end

class Post < ActiveRecord::Base
  attr_accessible :title, :content
  belongs_to :author, :class_name => "User"
end

class Author < ActiveRecord::Base
  belongs_to :user
  has_many :recent_posts, :through => :user,
          :class_name => "Post",
          :limit => 3,
          :order => "updated_at desc"
end

recent_post はどのように行うべきですか? 生のSQL?

4

1 に答える 1

1

次のように、他のモデルで関連付けを指定するために使用する:sourceオプションが必要です。has_many

has_many :recent_posts, :through => :user, :source => :posts, :limit => 3, :order => 'updated_at desc'
于 2013-01-22T21:28:46.353 に答える