Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8027149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:43:46+00:00 2026-06-04T23:43:46+00:00

I’m using Rails 1.2.3 (yeah, I know) and am confused about how has_many works

  • 0

I’m using Rails 1.2.3 (yeah, I know) and am confused about how has_many works with respect to object persistence.

For the sake of example, I’ll use this as my declaration:

class User < ActiveRecord::Base
    has_many :assignments
end

class Assignment < ActiveRecord::Base
    belongs_to :user
end

As I understand it, this generates, among others, a method User#assignments.build, which creates an Assignment object whose user_id is the receiving instance’s id (and whose other fields are as specified in the argument), but does not save this object in the database. The object can be saved later by calling Assignment#save!.

However, The Pragmatic Programmers’ Agile Web Development with Rails, Second Edition, which I’ve been using as a tutorial and reference, says:

If the parent object exists in the database, then adding a child
object to a collection automatically saves that child.

There seems to be a contradiction here. What I’d like to know is:

  • If I do some_user.assignments.build, is the Assignment object saved?
  • If I do some_user.assignments << Assignment.new, is the Assignment object saved?
  • If I do some_user.assignments << Assignment.create, are two database calls made, or just one? What about if I modify the Assignment object between creating it and adding it to some_user.assignments?
  • What happens if I save! an Assignment object whose corresponding User has not yet been saved in the database?

P.S. The reason I don’t just use User#assignments.create for everything is because it doesn’t let me farm out initialization to an external method, which I’d like to be able to do. I also don’t want to make multiple trips to the database.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:43:47+00:00Added an answer on June 4, 2026 at 11:43 pm

    NOTE: All the console tests below are run in Rails 3. You might get a different output in Rails 1, you’ll have to run the tests yourself to compare.

    Having your rails console handy is extremely valuable if you want to understand what’s happening behind the scenes with Active Record. Here’s what happens with a non saved object:

    u = User.new
    #<User id: nil, name: nil, created_at: nil, updated_at: nil> 
    
    u.assignments.build(:name => "example")
    #<Assignment id: nil, name: "example", user_id: nil, created_at: nil, updated_at: nil>
    
    u.save
    #SQL (0.2ms)  INSERT INTO `users` (`created_at`, `name`, `updated_at`) VALUES ('2012-06-01 19:25:45', NULL, '2012-06-01 19:25:45')
    #SQL (0.2ms)  INSERT INTO `assignments` (`created_at`, `name`, `updated_at`, `user_id`) VALUES ('2012-06-01 19:25:45', 'example', '2012-06-01 19:25:45', 1)
    

    As you can see, both are saved at the same time when the new user was saved. Now let’s try scenario two:

    u = User.create!(:name => "test")
    #SQL (0.2ms)  INSERT INTO `users` (`created_at`, `name`, `updated_at`) VALUES ('2012-06-01 19:27:21', 'test', '2012-06-01 19:27:21')
    #<User id: 2, name: "test", created_at: "2012-06-01 19:27:21", updated_at: "2012-06-01 19:27:21"> 
    
    u.assignments.build(:name => "example")
    #<Assignment id: nil, name: "example", user_id: 2, created_at: nil, updated_at: nil>
    

    So, from this we can conclude:

    If I do some_user.assignments.build, is the Assignment object saved?

    Nope

    If I do some_user.assignments << Assignment.new, is the Assignment object saved?

    No. This is exactly what assignments.build does, no difference.

    If I do some_user.assignments << Assignment.create, are two database calls made, or just one?

    Just the assignments.

    What about if I modify the Assignment object between creating it and adding it to some_user.assignments?

    Don’t understand, sorry.

    What happens if I save! an Assignment object whose corresponding User has not yet been saved in the database?

    It is saved to the database without a user_id. When you then call save on your user, an update command is issued to the assignment to add in the user id. Here it is in console:

    u = User.new(:name => "John Doe")
    #<User id: nil, name: "John Doe", created_at: nil, updated_at: nil> 
    
    a = Assignment.new(:name => "test")
    #<Assignment id: nil, name: "test", user_id: nil, created_at: nil, updated_at: nil> 
    
    u.assignments << a
    #[#<Assignment id: nil, name: "test", user_id: nil, created_at: nil, updated_at: nil>] 
    
    a.save!
    #SQL (0.2ms)  INSERT INTO `assignments` (`created_at`, `name`, `updated_at`, `user_id`) VALUES ('2012-06-01 19:33:24', 'test', '2012-06-01 19:33:24', NULL)
    
    a.user_id
    #nil 
    
    u.save!
    #INSERT INTO `users` (`created_at`, `name`, `updated_at`) VALUES ('2012-06-01 19:33:36', 'John Doe', '2012-06-01 19:33:36')
    #UPDATE `assignments` SET `user_id` = 3, `updated_at` = '2012-06-01 19:33:36' WHERE `assignments`.`id` = 3
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
We're building an app, our first using Rails 3, and we're having to build
i got an object with contents of html markup in it, for example: string
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.