CollectionProxy vs AssociationRelation

后端 未结 2 887
长情又很酷
长情又很酷 2021-01-01 18:54

I am wondering about the difference between ActiveRecord::Associations::CollectionProxy and ActiveRecord::AssociationRelation.

clas         


        
相关标签:
2条回答
  • 2021-01-01 19:03

    ActiveRecord::Relation are simple query objects before being turned into a query and executed, CollectionProxy on the other hand are a little bit more sophisticated.

    First of all you get association extentions, you probably saw something that looks like this, assume a book store model that has many books

    class Store < ActiveRecord::Base
      has_many :books do
        def used
          where(is_used: true)
        end
      end
    end
    

    This way you get to call used books in a store using a syntax that looks like this

    Store.first.books.used
    

    But this is the most basic uses, you could use the attributes that are exposed to you in the collection proxy, which are owner, reflection and target

    Owner

    The owner provides a a reference to the parent object holding the association

    Reflection

    The reflection object is an instance of ActiveRecord::Reflection::AssocciationReflection and contains all the configuration options for the association.

    Target

    The target is the association collection objects (or single object when has_one and belongs_to).

    Using those methods you could do some conditons in your association extention, for example if we have a blog, we give access to all deleted posts to users who are admins (lame example I know)

    Class Publisher < ActiveRecord::Base
      has_many :posts do
        def deleted
          if owner.admin?
            Post.where(deleted: true)
          else
            where(deleted: true)
          end
        end
      end
    end
    

    You also get access to two more methods which are reset and reload, the first one (reset) clears the cached association objects, the second one (reload) is more common and is used to reset and then loads the associated objects from the database.

    I hope this explains how having a CollectionProxy class would be so useful

    0 讨论(0)
  • 2021-01-01 19:23

    Ok. The difference is pretty simple.

    Explanation based on your example:

    the association proxy in v.wheels has:

    • the object in v as @owner;
    • the collection of its wheels as @target;
    • and the @reflection object represents a :has_many macro.

    From docs:

    Association proxies in Active Record are middlemen between the @owner and the @target. The @target object is not loaded until needed.

    v = Vehicle.new
    v.wheels # we are not sending any methods to @target object (collection of wheels)
    # => #<ActiveRecord::Associations::CollectionProxy []>
    

    Which means, as soon as you call any method on the @target object (that holds collection of wheels in our case) the @target is loaded, and it becomes ActiveRecord_AssociationRelation.

    v.wheels.all # sending the `all` method to @target (wheels)
    # => #<ActiveRecord::AssociationRelation []>
    
    0 讨论(0)
提交回复
热议问题