Expanding on recent_posts_on_self below, I want to add an all_recent_posts_on_self method but I'm not sure if it's possible using the syntax self.posts.find. On the other hand, all_recent_posts_on_class seems straightforward.
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
In the example above, I have two ways of finding the recent blog posts that are associated with a user. I can call Post.find and pass it the author_id or I can call self.posts.find and I don't need to pass an author id. I assume this is because in the latter case, self.posts has already been limited based on the primary key of the user object and the has_many :posts associated with this user. This is an advantage in this case because I don't need to go to the trouble of passing author_id as an argument. But if I did not need to limit the query by author, would it be possible to create an all_recent_posts_on_self to do this?
What I'm talking about is an equivalent of this method (which omits the :conditions):
def all_recent_posts_on_class
Post.find(:all, :order => 'created_at asc', :limit => 5)
end
But using self.posts.find instead of Post.find:
def all_recent_posts_on_self
self.posts.find(...)
end
Also:
Even if it's possible to use self.posts.find to do this, is it "better" to use Post.find?