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

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

create!

create!(attributes = {}, &block) Instance Public methods Like create, except that if the record is invalid, raises an exception. class Person has_many :pets end class Pet validates :name, presence: true end person.pets.create!(name: nil) # => ActiveRecord::RecordInvalid: Validation failed: Name can't be blank

create

create(attributes = {}, &block) Instance Public methods Returns a new object of the collection type that has been instantiated with attributes, linked to this object and that has already been saved (if it passes the validations). class Person has_many :pets end person.pets.create(name: 'Fancy-Fancy') # => #<Pet id: 1, name: "Fancy-Fancy", person_id: 1> person.pets.create([{name: 'Spook'}, {name: 'Choo-Choo'}]) # => [ # #<Pet id: 2, name: "Spook", person_

count

count(column_name = nil, options = {}) Instance Public methods Count all records using SQL. class Person < ActiveRecord::Base has_many :pets end person.pets.count # => 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-Choo", person_id: 1> # ]

concat

concat(*records) Instance Public methods Add one or more records to the collection by setting their foreign keys to the association's primary key. Since << flattens its argument list and inserts each record, push and concat behave identically. Returns self so method calls may be chained. class Person < ActiveRecord::Base has_many :pets end person.pets.size # => 0 person.pets.concat(Pet.new(name: 'Fancy-Fancy')) person.pets.concat(Pet.new(name: 'Spook'), Pet.new(name:

clear

clear() Instance Public methods Equivalent to delete_all. The difference is that returns self, instead of an array with the deleted objects, so methods can be chained. See delete_all for more information.

build

build(attributes = {}, &block) Instance Public methods Returns a new object of the collection type that has been instantiated with attributes and linked to this object, but have not yet been saved. You can pass an array of attributes hashes, this will return an array with the new objects. class Person has_many :pets end person.pets.build # => #<Pet id: nil, name: nil, person_id: 1> person.pets.build(name: 'Fancy-Fancy') # => #<Pet id: nil, name: "Fancy-Fancy"

arel

arel() Instance Public methods

append

append(*records) Instance Public methods Alias for: <<