<<

<<(*records)
Instance Public methods

Adds one or more records to the collection by setting their foreign keys to the association's primary key. Returns self, so several appends may be chained together.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Person < ActiveRecord::Base
  has_many :pets
end
 
person.pets.size # => 0
person.pets << Pet.new(name: 'Fancy-Fancy')
person.pets << [Pet.new(name: 'Spook'), Pet.new(name: 'Choo-Choo')]
person.pets.size # => 3
 
person.id # => 1
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>
#    ]

push append

doc_ruby_on_rails
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.