How can I group / reuse named scopes in Rails?

How do I group realm names? For example, I have two models: "User and Activity". The user can have many actions.

Activity has two namespaces:

Activity.ordered_by_created_at
Activity.top_20

      

I want to create a new named area Activity.recent

so that

Activity.recent == Activity.ordered_by_created_at.top_20

      

This way I can call recently on user.activities

.

user.activities.recent

      

Is it possible? Thanks.

0


source to share


2 answers


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



It might be worth considering changing the a la Activtiy.recent.user (UID) approach and just write named_scope in the activity.rb file that finds related users with the activity.

0


source


Perhaps you can do this:



class Activity < ActiveRecord::Base
  def self.recent
    ordered_by_created_at.top_20
  end
end

      

0


source







All Articles