Giancarlos
Giancarlos

Reputation: 151

How add a record in a collection with rails console?

i have this :

 Order.find(19).order_items

 => #<ActiveRecord::Associations::CollectionProxy []> 

how can i add this object "order_item" to "Order"?

order_item = OderItem.find(2)

Upvotes: 0

Views: 296

Answers (1)

Uelb
Uelb

Reputation: 4093

There is your answer :

 Order.find(19).order_items << OrderItem.find(2)

After reading some comments, let me improve my answer by adding this link : http://guides.rubyonrails.org/association_basics.html#has-many-association-reference You can look at the 4.3.1.2 collection<<(object, ...) point

Upvotes: 2

Related Questions