delete

delete(*records) Instance Public methods Deletes the records supplied and removes them from the collection. For has_many associations, the deletion is done according to the strategy specified by the :dependent option. Returns an array with the deleted records. If no :dependent option is given, then it will follow the default strategy. The default strategy is :nullify. This sets the foreign keys to NULL. For, has_many :through, the default strategy is delete_all. class Person < A

delete_all

delete_all(dependent = nil) Instance Public methods Deletes all the records from the collection. For has_many associations, the deletion is done according to the strategy specified by the :dependent option. Returns an array with the deleted records. If no :dependent option is given, then it will follow the default strategy. The default strategy is :nullify. This sets the foreign keys to NULL. For, has_many :through, the default strategy is delete_all. class Person < ActiveRecord

destroy

destroy(*records) Instance Public methods Destroys the records supplied and removes them from the collection. This method will always remove record from the database ignoring the :dependent option. Returns an array with the removed records. class Person < ActiveRecord::Base has_many :pets end person.pets.size # => 3 person.pets # => [ # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>, # #<Pet id: 2, name: "Spook", person_id: 1>, # #<Pet

destroy_all

destroy_all() Instance Public methods Deletes the records of the collection directly from the database ignoring the :dependent option. It invokes before_remove, after_remove , before_destroy and after_destroy callbacks. class Person < ActiveRecord::Base has_many :pets end person.pets.size # => 3 person.pets # => [ # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>, # #<Pet id: 2, name: "Spook", person_id: 1>, # #<Pet id: 3, name: "Choo-Ch

distinct

distinct() Instance Public methods Specifies whether the records should be unique or not. class Person < ActiveRecord::Base has_many :pets end person.pets.select(:name) # => [ # #<Pet name: "Fancy-Fancy">, # #<Pet name: "Fancy-Fancy"> # ] person.pets.select(:name).distinct # => [#<Pet name: "Fancy-Fancy">] uniq

empty?

empty?() Instance Public methods Returns true if the collection is empty. If the collection has been loaded it is equivalent to collection.size.zero?. If the collection has not been loaded, it is equivalent to collection.exists?. If the collection has not already been loaded and you are going to fetch the records anyway it is better to check collection.length.zero?. class Person < ActiveRecord::Base has_many :pets end person.pets.count # => 1 person.pets.empty? # => fa

fifth

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

find

find(*args, &block) Instance Public methods Finds an object in the collection responding to the id. Uses the same rules as ActiveRecord::Base.find. Returns ActiveRecord::RecordNotFound error if the object cannot be found. 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>

first

first(*args) Instance Public methods Returns the first record, or the first n records, from the collection. If the collection is empty, the first form returns nil, and the second form returns an empty array. 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.pe

forty_two

forty_two(*args) Instance Public methods Same as first except returns only the forty second record. Also known as accessing âthe redditâ.