Hi again Josh,
Is there any specific reason why it would be better to keep this in the
User model instead of just having it in my Task model?
One of the reasons I was trying to do it this way was so I could just
put:
"Task.most_recent" and find ALL of the most recent tasks
and then still use the same method for individual users and put
"Task.most_recent(josh.tasks)"
but your definition :
def self.most_recent(tasks = self)
tasks.find(:all, :order => 'updated_at DESC', :limit => 10)
end
works but is quite ugly, in the sense it's not the Ruby way.
If you want to share your AR query, put it in a module, then mix
it in the classes you want :
module Stuff
def most_recent(n = 10)
find :all, :order => 'updated_at DESC', :limit => n
end
end
class User < AR::B
class << self
include Stuff
end
has_many :tasks, :extend => Stuff
end
class Task
class << self
include Stuff
end
end
class Comment < AR::B
class << self
include Stuff
end
end
Then you can do Comment.most_recent, josh.tasks.most_recent
Task.most_recent, Task.most_recent(20)
Modules are the way to make it DRY.
I see how the methods you have posted would help a lot
if I was only finding a collection of tasks relating to the user,
but with mine, i could pass any collection i wanted or just
not give it any paramters to search the entire database.
and with your method, sth like Task.most_recent(Comment)
would return the last recent comments, but since Comment model
is not necessarily related to Task model, this is quite ugly for me !
Regards,
-- Jean-François.