Rails used an ORM (Object Relational Mapper) called Active Record that has a lot of cool features, one of these is scopes.
A scope is essentially a SQL WHERE
clause and it is written as below
class Book < ActiveRecord::Base
scope :recent, -> { where('finished_on > ?',2.days.ago)}
end
so running Book.recent
in rails console
or in your app will give you all Books with the recent value being within the
last two days.
But what is a scope? If you think of each row in the Books table as an instance of the Book
class. Then
the scope is simply a class method. So writing the scope as below is almost equivalent
class Book < ActiveRecord::Base
def self.recent
where('finished_on > ?',2.days.ago)
end
end
almost… there are a few subtle differences