<< does it force a save?

my tests seem to imply that << forces an insert of the parent and child
records in a has_many relationship when both are newly created objects.

for example,

Controller code:


Model code:

Class XY < ActiveRecord…
def addPC(pc)

where XY has a “has_many” association with PC and PC has a “belongs_to”

I note that two INSERTs are performed into the tables behind PC and XY
models as a result of the call to addPC() but I’m not issuing a .save
anywhere in my code.

Is this normal behaviour?

Found the answer in the Rails API site:
* Adding an object to a collection (has_many or
has_and_belongs_to_many) automatically saves that object, except if the
parent object (the owner of the collection) is not yet stored in the

In my case the parent object (xy) had been saved through another
association :slight_smile: thus causing an automatic save of the pc object.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs