Type:
Class

Association proxies in Active Record are middlemen between the object that holds the association, known as the @owner, and the actual associated object, known as the @target. The kind of association any proxy is about is available in @reflection. That's an instance of the class ActiveRecord::Reflection::AssociationReflection.

For example, given

1
2
3
4
5
class Blog < ActiveRecord::Base
  has_many :posts
end
 
blog = Blog.first

the association proxy in blog.posts has the object in blog as @owner, the collection of its posts as @target, and the @reflection object represents a :has_many macro.

This class delegates unknown methods to @target via method_missing.

The @target object is not loaded until needed. For example,

1
blog.posts.count

is computed directly through SQL and does not trigger by itself the instantiation of the actual post records.

reset
  • References/Ruby on Rails/Rails/Classes/ActiveRecord/ActiveRecord::Associations/ActiveRecord::Associations::CollectionProxy

reset() Instance Public methods Unloads the association. Returns self

2025-01-10 15:47:30
delete_all
  • References/Ruby on Rails/Rails/Classes/ActiveRecord/ActiveRecord::Associations/ActiveRecord::Associations::CollectionProxy

delete_all(dependent = nil) Instance Public methods Deletes all the records

2025-01-10 15:47:30
clear
  • References/Ruby on Rails/Rails/Classes/ActiveRecord/ActiveRecord::Associations/ActiveRecord::Associations::CollectionProxy

clear() Instance Public methods Equivalent to delete_all. The difference

2025-01-10 15:47:30
proxy_association
  • References/Ruby on Rails/Rails/Classes/ActiveRecord/ActiveRecord::Associations/ActiveRecord::Associations::CollectionProxy

proxy_association() Instance Public methods

2025-01-10 15:47:30
select
  • References/Ruby on Rails/Rails/Classes/ActiveRecord/ActiveRecord::Associations/ActiveRecord::Associations::CollectionProxy

select(*fields, &block) Instance Public methods Works in two ways.

2025-01-10 15:47:30
create!
  • References/Ruby on Rails/Rails/Classes/ActiveRecord/ActiveRecord::Associations/ActiveRecord::Associations::CollectionProxy

create!(attributes = {}, &block) Instance Public methods Like create

2025-01-10 15:47:30
last
  • References/Ruby on Rails/Rails/Classes/ActiveRecord/ActiveRecord::Associations/ActiveRecord::Associations::CollectionProxy

last(*args) Instance Public methods Returns the last record, or the last n

2025-01-10 15:47:30