views:

72

answers:

2

How do I group named scopes? For example, I have two models, User and Activity. A user can have many activities.

Activity has two named scopes:

Activity.ordered_by_created_at
Activity.top_20

I want to create a new named scope Activity.recent such that

Activity.recent == Activity.ordered_by_created_at.top_20

That way, I can call recent on user.activities.

user.activities.recent

Is this possible? Thanks.

A: 

You can probably do this:

class Activity < ActiveRecord::Base
  def self.recent
    ordered_by_created_at.top_20
  end
end
August Lilleaas
Yeah, that's what I did. But it doesn't work for `user.activities.recent`
gsmendoza
A: 

When you call user.activities, assuming you have a has_many :activities relationship in your user.rb file, ActiveRecord will return an array of all the users activities.

Might want to consider changing the approach a la Activtiy.recent.user(UID) and just write a named_scope in your activity.rb file that finds associated users with the activity.

Scott
Hmmm... Yeah, Activity.find_by_user_id(user.id).recent is probably the way to do it. Thanks.
gsmendoza