Does ActiveRecord save a belongs_to association when saving main object?
Solution 1
ActiveRecord belongs_to
associations have the ability to be autosaved along with the parent model, but the functionality is off by default. To enable it:
class Post < ActiveRecord::Base
belongs_to :user, :autosave => true
end
Solution 2
I believe you want:
class User < ActiveRecord::Base
has_many :posts, :autosave => true
end
In other words, when saving a User record, seek out all records on the other side of the 'posts' association and save them.
Solution 3
The belongs_to API documentation says (Rails 4.2.1):
:autosave
If true, always save the associated object or destroy it if marked for destruction, when saving the parent object.
If false, never save or destroy the associated object.
By default, only save the associated object if it’s a new record.
Note that accepts_nested_attributes_for sets :autosave to true.
In your case user is new record so it will be auto saved.
The last sentence about accepts_nested_attributes_for
is also missed by many.
agentofuser
I'm a software engineer with 15+ years of experience. I have written front-end and back-end code used by millions of people while at Qype GmbH (acquired by Yelp). I'm currently specializing in React, IPFS, and functional programming. I have designed, implemented, and marketed products from the ground up, lead teams of engineers, and founded my own startups, one them funded in the Start-Up Chile program. I have worked remotely for many years, both on open source projects (3x participant in the Google Summer of Code program) and commercial ones. I'm a good communicator and team member. I'm self-directed and disciplined with my time. I studied Computer Engineering at Brazil's (often ranked as) #1 higher education and research institution (both in general and specifically in Computer Science): University of Campinas. I've lived for almost 2 years in the United States. I can read, write, and speak English fluently. Feel free to get in touch at [email protected]. More contact information at https://agentofuser.com.
Updated on June 05, 2022Comments
-
agentofuser almost 2 years
If I have two models:
class Post < ActiveRecord::Base belongs_to :user end
and
class User < ActiveRecord::Base has_many :posts end
If I do:
post = Post.new user = User.new post.user = user post.save
Does the user get saved as well and the primary key properly assigned in
post
'suser_id
field? -
agentofuser about 14 yearsWeird. I turned that flag on, and doing the same as above still gives me
> post.errors #=> #<OrderedHash {:user_id=>["can't be blank"]}>
anduser.new_record? #=> true
. Am I missing something? -
Ode over 11 yearsActually the functionality is on by default. You must set it to false in order to turn it off, otherwise all associations will be saved automatically.
-
Francesco Belladonna about 11 years@OdeeOdum: That's not true, I had a problem like this and setting autosave: true in rails 3 fixed the issue.
-
Ode about 11 years@Fire-Dragon-DoL, Actually it is the default behavior. According to the ActiveRecord::AutosaveAssocation documentation When the :autosave option is not present new associations are saved.
-
Francesco Belladonna about 11 yearsMhhh, I need to check, because I tried without it and it wasn't working. When I added the autosave option it started working fine, and I didn't change anything else meanwhile.
-
Christian almost 11 yearsFrom the ActiveRecord docs:
Note that autosave: false is not same as not declaring :autosave. When the :autosave option is not present then new association records are saved but the updated association records are not saved.