target

target() Instance Public methods

take

take(n = nil) Instance Public methods

spawn

spawn() Instance Public methods Alias for: scope

size

size() Instance Public methods Returns the size of the collection. If the collection hasn't been loaded, it executes a SELECT COUNT(*) query. Else it calls collection.size. If the collection has been already loaded size and length are equivalent. If not and you are going to need the records anyway length will take one less query. Otherwise size is more efficient. class Person < ActiveRecord::Base has_many :pets end person.pets.size # => 3 # executes something like SELECT C

select

select(*fields, &block) Instance Public methods Works in two ways. First: Specify a subset of fields to be selected from the result set. class Person < ActiveRecord::Base has_many :pets end person.pets # => [ # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>, # #<Pet id: 2, name: "Spook", person_id: 1>, # #<Pet id: 3, name: "Choo-Choo", person_id: 1> # ] person.pets.select(:name) # => [ # #<Pet id: nil, name: "Fancy-Fa

second

second(*args) Instance Public methods Same as first except returns only the second record.

scoping

scoping() Instance Public methods We don't want this object to be put on the scoping stack, because that could create an infinite loop where we call an @association method, which gets the current scope, which is this object, which delegates to @association, and so on.

scope

scope() Instance Public methods Returns a Relation object for the records in this association spawn

reset

reset() Instance Public methods Unloads the association. Returns self. class Person < ActiveRecord::Base has_many :pets end person.pets # fetches pets from the database # => [#<Pet id: 1, name: "Snoop", group: "dogs", person_id: 1>] person.pets # uses the pets cache # => [#<Pet id: 1, name: "Snoop", group: "dogs", person_id: 1>] person.pets.reset # clears the pets cache person.pets # fetches pets from the database # => [#<Pet id: 1, name: "Snoop",

replace

replace(other_array) Instance Public methods Replaces this collection with other_array. This will perform a diff and delete/add only records that have changed. class Person < ActiveRecord::Base has_many :pets end person.pets # => [#<Pet id: 1, name: "Gorby", group: "cats", person_id: 1>] other_pets = [Pet.new(name: 'Puff', group: 'celebrities'] person.pets.replace(other_pets) person.pets # => [#<Pet id: 2, name: "Puff", group: "celebrities", person_id: 1>