Add example code to Scopes docs in README

[skip ci]
This commit is contained in:
Jon Atack 2014-09-10 23:59:12 +02:00
parent 620f53eb29
commit 6b6f232600
1 changed files with 16 additions and 0 deletions

View File

@ -401,6 +401,22 @@ methods (e.g. scopes) are ignored. Scopes will be applied for matching `true`
values, or for given values if the scope accepts a value:
```ruby
class Employee < ActiveRecord::Base
scope :active, -> { where(active: true) }
def self.hired_since(date = Date.current)
where('start_date >= ?', date)
end
def self.salary_gt(amount)
where('salary > ?', amount)
end
private
def self.ransackable_scopes(auth_object = nil)
%i(active hired_since salary_gt)
end
Employee.search({ active: true, hired_since: '2013-01-01' })
Employee.search({ salary_gt: 100_000 }, { auth_object: current_user })