Rails ActiveRecord Scopes With Arguments

Ask questions Research chat →

https://teamgaslight.com/blog/rails-activerecord-scopes-with-arguments · scraped

rails

Attachments

Scraped Content

— 363 words · 2026-02-14 17:41:17 UTC ·

Excerpt

I came across another interesting syntax used in a Rails application. ```plain text scope :graduated_after, ->(date) { where('graduation_date > ?', date) } ``` Method ‘scope’ takes two arguments: - name of the scope - defined as symbol value (not as a string value) - a lambda function that implements the query Method ‘scope’ can be used with or without an argument. In our example above we want to get a list of students who graduated after a certain year. The year is passed as an argument. If we needed a list of students who graduated during the last calendar year then we could skip passing the argument: ```plain text scope :graduated_within_a_year, -> { where('graduation_date > ?', 1.year.ago) } ``` Or, we can also use default value for the argument: ```plain text scope :graduated_after, ->(date = 2015) { where('graduation_date > ?', date) } ``` Rails scopes have several benefits compared to model class methods: unlike model class methods Rails scopes can be chained with oth
I came across another interesting syntax used in a Rails application. ```plain text scope :graduated_after, ->(date) { where('graduation_date > ?', date) } ``` Method ‘scope’ takes two arguments: - name of the scope - defined as symbol value (not as a string value) - a lambda function that implements the query Method ‘scope’ can be used with or without an argument. In our example above we want to get a list of students who graduated after a certain year. The year is passed as an argument. If we needed a list of students who graduated during the last calendar year then we could skip passing the argument: ```plain text scope :graduated_within_a_year, -> { where('graduation_date > ?', 1.year.ago) } ``` Or, we can also use default value for the argument: ```plain text scope :graduated_after, ->(date = 2015) { where('graduation_date > ?', date) } ``` Rails scopes have several benefits compared to model class methods: unlike model class methods Rails scopes can be chained with other scopes Rails scopes can be merged with custom ActiveRecord queries Rails scopes can be called either with a class or on an association consisting of that class’s objects For example, we can define another Rails scope, like this: With these two scopes defined we can easily make a query like ```plain text Student.GPA_higher_than(3.5).graduated_after(2015) ``` If we add two model class methods with names ‘GPA_higher_than’ and ‘graduated_after’ those methods would not work if chained. This means that using scopes we can do separate queries ```plain text recently_graduated = Student.graduated_after(2015) recently_graduated.GPA_higher_than(3.5) ``` Alternatively, because ‘recently_graduated’ is an ActiveRecord::Relation we also could do ```plain text threshold = 3.5 recently_graduated.where('gpa > ?', threshold) ``` So far we used the ‘where’ query method in defining our scopes. However, we can use any other ActiveRecord query method with Rails scopes. The following scope selects only needed attributes. ```plain text scope :get_name_and_major, -> { select(:id, :name, :major) } ``` We can also set a default scope for model classes that inherit from ActiveRecord. For example, when we process records related to students we may want to ignore all students who have dropped from the course. If that is the case we could use the following default_scope

Visibility

Visible to everyone

Reading Status

Related Bookmarks

My Note


Saved!

Annotations

Export as Markdown
+ Annotate selection

Add Annotation