For has_many
, the link information is stored in the target record. This means that <<
will have to modify this entry to add it to the set.
Perhaps with the intention of convenience, ActiveRecord automatically saves them for you when completing the task, if the task was successful. The exception is for new records, the record with which they are associated does not have an identifier, so it must be deferred. They are saved when the record with which they are associated is finally created.
This may be a bit confusing, perhaps unexpected, but in fact this is what you would like to do 99% of the time. If you do not want this to happen, you must manually manipulate the link:
property = Property.find(params[:saved_property_id]) property.user = @user property.save!
This is basically equivalent, but much more verbose.
source share