In active_record/base.rb, module ActiveRecord you can see this code:
delegate :find, :first, :last, :all, :destroy, :destroy_all, :exists?, :delete,
:delete_all, :update, :update_all, :to => :scoped
Let’s take first method, so i assumed that first method delegates to scoped method and then scoped should return the first record from database. But scoped is just the anonymous scope, how the current construction is doing its job?
At the same time, how dynamic methods work, like find_by_name, find_all_by_name_and_colour?
Thanks
According to the documentation,
delegate:So this delegates the methods in the list to the
scopedmethod, which is defined in ActiveRecord::NamedScoped::ClassMethods, and which returns an anonymous scope.As to why ActiveRecord does this, it is so we can continue to use familiar methods such as
findwhile behind the scenes AR is actually calling the fancy new Arel methods. For instance, when you doWhat actually gets executed is: