instance method
build
Ruby on Rails 4.0.13
Since v4.0.13Signature
build(attributes = {}, &block)
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", person_id: 1> person.pets.build([{name: 'Spook'}, {name: 'Choo-Choo'}, {name: 'Brain'}]) # => [ # #<Pet id: nil, name: "Spook", person_id: 1>, # #<Pet id: nil, name: "Choo-Choo", person_id: 1>, # #<Pet id: nil, name: "Brain", person_id: 1> # ] person.pets.size # => 5 # size of the collection person.pets.count # => 0 # count from database
Parameters
-
attributesopt = {} -
blockblock
Source
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 228
def build(attributes = {}, &block)
@association.build(attributes, &block)
end
Defined in activerecord/lib/active_record/associations/collection_proxy.rb line 228
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Associations::CollectionProxy