ruby - Understanding delegate and scoped methods in Rails -
in active_record/base.rb, module activerecord can see code:
delegate :find, :first, :last, :all, :destroy, :destroy_all, :exists?, :delete, :delete_all, :update, :update_all, :to => :scoped let's take first method, assumed first method delegates scoped method , scoped should return first record database. scoped anonymous scope, how current construction doing job?
at same time, how dynamic methods work, find_by_name, find_all_by_name_and_colour?
thanks
according documentation, delegate:
provides delegate class method expose contained objects’ methods own. pass 1 or more methods (specified symbols or strings) , name of target object via :to option (also symbol or string)
so delegates methods in list scoped method, defined in activerecord::namedscoped::classmethods, , returns anonymous scope.
as why activerecord this, can continue use familiar methods such find while behind scenes ar calling fancy new arel methods. instance, when do
post.find(37) what gets executed is:
post.where(primary_key.eq(37))
Comments
Post a Comment