Does ActiveRecord save a belongs_to association when saving main object?

11,970

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.

Share:
11,970
agentofuser
Author by

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, 2022

Comments

  • agentofuser
    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's user_id field?

  • agentofuser
    agentofuser about 14 years
    Weird. I turned that flag on, and doing the same as above still gives me > post.errors #=> #<OrderedHash {:user_id=>["can't be blank"]}> and user.new_record? #=> true. Am I missing something?
  • Ode
    Ode over 11 years
    Actually 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
    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
    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
    Francesco Belladonna about 11 years
    Mhhh, 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
    Christian almost 11 years
    From 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.