以下の recent_posts_on_self を拡張すると、 all_recent_posts_on_self メソッドを追加したいのですが、構文 self.posts.find を使用して可能かどうかわかりません。一方、all_recent_posts_on_class は単純に見えます。
class User < ActiveRecord::Base
has_many :posts, :class_name => "Post" , :foreign_key => "author_id"
has_many :comments, :class_name => "Comment", :foreign_key => "author_id"
def recent_posts_on_class
Post.find( :all, :conditions => ['author_id = ?', self.id],
:order => 'created_at asc', :limit => 5)
end
def recent_posts_on_self
self.posts.find(:all, :order => 'created_at ASC', :limit => 5)
end
end
上記の例では、ユーザーに関連付けられている最近のブログ投稿を見つける方法が 2 つあります。Post.find を呼び出して author_id を渡すか、self.posts.find を呼び出すことができ、著者 ID を渡す必要はありません。これは、後者の場合、ユーザー オブジェクトの主キーと、このユーザーに関連付けられた has_many :posts に基づいて、self.posts が既に制限されているためだと思います。これは、この場合の利点です。なぜなら、author_id を引数として渡すという手間をかける必要がないからです。しかし、作成者ごとにクエリを制限する必要がなければ、これを行うために all_recent_posts_on_self を作成することは可能でしょうか?
私が話しているのは、このメソッドに相当するものです(:conditions を省略しています):
def all_recent_posts_on_class
Post.find(:all, :order => 'created_at asc', :limit => 5)
end
ただし、 Post.find の代わりに self.posts.find を使用する:
def all_recent_posts_on_self
self.posts.find(...)
end
また:
これを行うために self.posts.find を使用することは可能ですが、Post.find を使用する方が「良い」のでしょうか?