options
hash containing :conditions
:include
:joins
:limit
:offset
:order
:select
:readonly
:group
having
:from
:lock
any method of the class provided by ActiveRecord ' This is now considered obsolete.
find(id_or_array_of_ids, options) find(:first, options) find(:all, options) first(options) all(options) update_all(updates, conditions, options)
find(id_or_array_of_ids, options) find(:first, options) find(:all, options) first(options) all(options) update_all(updates, conditions, options)
find(id_or_array_of_ids, options) find(:first, options) find(:all, options) first(options) all(options) update_all(updates, conditions, options)
find(id_or_array_of_ids, options) find(:first, options) find(:all, options) first(options) all(options) update_all(updates, conditions, options)
find(id_or_array_of_ids, options) find(:first, options) find(:all, options) first(options) all(options) update_all(updates, conditions, options)
find(id_or_array_of_ids, options) find(:first, options) find(:all, options) first(options) all(options) update_all(updates, conditions, options)
count(column, options) average(column, options) minimum(column, options) maximum(column, options) sum(column, options) calculate(operation, column, options)
count(column, options) average(column, options) minimum(column, options) maximum(column, options) sum(column, options) calculate(operation, column, options)
count(column, options) average(column, options) minimum(column, options) maximum(column, options) sum(column, options) calculate(operation, column, options)
count(column, options) average(column, options) minimum(column, options) maximum(column, options) sum(column, options) calculate(operation, column, options)
count(column, options) average(column, options) minimum(column, options) maximum(column, options) sum(column, options) calculate(operation, column, options)
count(column, options) average(column, options) minimum(column, options) maximum(column, options) sum(column, options) calculate(operation, column, options)
find(:first)
and find(:all)
(without any additional options) will also be excluded in favor of first
and all
. As an exception to the rules, count()
will still accept the :distinct
option.
User.find(:all, :limit => 1)
User.find(:all)
User.find(:first)
User.first(:conditions => {:name => 'lifo'})
User.all(:joins => :items)
User.find(1)
User.find(1,2,3)
User.find_by_name('lifo')
named_scope
method will also lose support:
named_scope :red, :conditions => { :colour => 'red' }
named_scope :red, lambda {|colour| {:conditions => { :colour => colour }} }
with_scope
, with_exclusive_scope
and default_scope
:
with_scope(:find => {:conditions => {:name => 'lifo'}) { ... }
with_exclusive_scope(:find => {:limit =>1}) { ... }
default_scope :order => "id DESC"
scoped_by_
will likewise go down in history:red_items = Item.scoped_by_colour('red')
red_old_items = Item.scoped_by_colour_and_age('red', 2)
options
hash is shown in parentheses):where (:conditions) select group order limit joins includes (:include) lock readonly from
where (:conditions) select group order limit joins includes (:include) lock readonly from
where (:conditions) select group order limit joins includes (:include) lock readonly from
where (:conditions) select group order limit joins includes (:include) lock readonly from
where (:conditions) select group order limit joins includes (:include) lock readonly from
where (:conditions) select group order limit joins includes (:include) lock readonly from
where (:conditions) select group order limit joins includes (:include) lock readonly from
where (:conditions) select group order limit joins includes (:include) lock readonly from
where (:conditions) select group order limit joins includes (:include) lock readonly from
where (:conditions) select group order limit joins includes (:include) lock readonly from
Relation
class. In principle, Relation
very similar to anonymous named_scope
. All of these methods are also defined in it itself, which makes it possible to create call chains:lifo = User.where(:name => 'lifo')
new_users = User.order('users.id DESC').limit(20).includes(:items)
cars = Car.where(:colour => 'black')
rich_ppls_cars = cars.order('cars.price DESC').limit(10)
new (attributes) create (attributes) create! (attributes) find (id_or_array) destroy (id_or_array) destroy_all delete (id_or_array) delete_all update (ids, updates) update_all (updates) exists?
new (attributes) create (attributes) create! (attributes) find (id_or_array) destroy (id_or_array) destroy_all delete (id_or_array) delete_all update (ids, updates) update_all (updates) exists?
new (attributes) create (attributes) create! (attributes) find (id_or_array) destroy (id_or_array) destroy_all delete (id_or_array) delete_all update (ids, updates) update_all (updates) exists?
new (attributes) create (attributes) create! (attributes) find (id_or_array) destroy (id_or_array) destroy_all delete (id_or_array) delete_all update (ids, updates) update_all (updates) exists?
new (attributes) create (attributes) create! (attributes) find (id_or_array) destroy (id_or_array) destroy_all delete (id_or_array) delete_all update (ids, updates) update_all (updates) exists?
new (attributes) create (attributes) create! (attributes) find (id_or_array) destroy (id_or_array) destroy_all delete (id_or_array) delete_all update (ids, updates) update_all (updates) exists?
new (attributes) create (attributes) create! (attributes) find (id_or_array) destroy (id_or_array) destroy_all delete (id_or_array) delete_all update (ids, updates) update_all (updates) exists?
new (attributes) create (attributes) create! (attributes) find (id_or_array) destroy (id_or_array) destroy_all delete (id_or_array) delete_all update (ids, updates) update_all (updates) exists?
new (attributes) create (attributes) create! (attributes) find (id_or_array) destroy (id_or_array) destroy_all delete (id_or_array) delete_all update (ids, updates) update_all (updates) exists?
new (attributes) create (attributes) create! (attributes) find (id_or_array) destroy (id_or_array) destroy_all delete (id_or_array) delete_all update (ids, updates) update_all (updates) exists?
new (attributes) create (attributes) create! (attributes) find (id_or_array) destroy (id_or_array) destroy_all delete (id_or_array) delete_all update (ids, updates) update_all (updates) exists?
red_items = Item.where(:colour => 'red')
red_items.find(1)
item = red_items.new
item.colour #=> 'red'
red_items.exists? #=> true
red_items.update_all :colour => 'black'
red_items.exists? #=> false
update
or delete
/ destroy
method will “reset” the Relation
, i.e. will remove cache entries used to optimize methods (such as relation.size
).
Relations
loaded “lazily” - i.e. above them it is necessary to call the methods of working with the collection. This is very similar to how associations
and named_scope
's already work.cars = Car.where(:colour => 'black') # Relations ,
cars.each {|c| puts c.name } # "select * cars ..."
def index
@recent_items = Item.limit(10).order('created_at DESC')
end
And in view:
<% cache('recent_items') do %>
<% @recent_items.each do |item| %>
...
<% end %>
<% end %>
@recent_items
filled from the database only at the moment of calling @recent_items.each
from the view
. Since the controller does not execute the query from the database, fragment caching becomes much more efficient without requiring any additional work.
all
, first
& last
all
on an object of the Relation type:
cars = Car.where(:colour => 'black').all
all
returns Array
, not Relation
. This is similar to the way named_scope
and associations
work in Rails 2.3 now.
first
and last
methods will return an object of type ActiveRecord
(or nil
):
cars = Car.order('created_at ASC')
oldest_car = cars.first
newest_car = cars.last
named_scope
→ scope
named_scope
method is considered obsolete in Rails 3.0, in favor of scope
. But the only thing that really changed is that now you don’t need to write the named_
prefix. Passing options for the search will be permanently excluded from Rails 3.1.
named_scope
method was simply renamed scope
. Those. the following definition:
class Item
named_scope :red, :conditions => { :colour => 'red' }
named_scope :since, lambda {|time| {:conditions => ["created_at > ?", time] }}
end
class Item scope: red,: conditions => {: color => 'red'} scope: since, lambda {| time | {: conditions => ["created_at>?", time]}} end
class Item scope: red, where (: color => 'red') scope: since, lambda {| time | where ("created_at>?", time)} end
named scope
's are add-ins over Relation
, thereby making very simple variations for use in conjunction with finder
methods:
red_items = Item.red
available_red_items = red_items.where("quantity > ?", 0)
old_red_items = Item.red.since(10.days.ago)
Model.scoped
Model.scoped
.
cars = Car.scoped
rich_ppls_cars = cars.order('cars.price DESC').limit(10)
white_cars = cars.where(:colour => 'red')
ActiveRecord::Base
now contains the following delegates:
delegate :find, :first, :last, :all, :destroy, :destroy_all, :exists?, :delete, :delete_all, :update, :update_all, :to => :scoped
delegate :select, :group, :order, :limit, :joins, :where, :preload, :eager_load, :includes, :from, :lock, :readonly, :having, :to => :scoped
delegate :count, :average, :minimum, :maximum, :sum, :calculate, :to => :scoped
ActiveRecord
. In addition to this, any dynamic methods, aka find_by_name
, find_all_by_name_and_colour
, are also delegated to the Relation
'y.
with_scope
and with_exclusive_scope
with_scope
and with_exclusive_scope
now built on top of Relation
'a, providing the ability to use any relation with them:
with_scope (where (: name => 'lifo')) do ... end
with_exclusive_scope (Item.red) do ... end
Source: https://habr.com/ru/post/82767/