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 214639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:23:37+00:00 2026-05-11T18:23:37+00:00

I have been unable to find any documentation on the .build method in Rails

  • 0

I have been unable to find any documentation on the .build method in Rails (i am currently using 2.0.2).

Through experimentation it seems you can use the build method to add a record into a has_many relationship before either record has been saved.

For example:

class Dog < ActiveRecord::Base
  has_many :tags
  belongs_to :person
end

class Person < ActiveRecord::Base
  has_many :dogs
end

# rails c
d = Dog.new
d.tags.build(:number => "123456")
d.save # => true

This will save both the dog and tag with the foreign keys properly. This does not seem to work in a belongs_to relationship.

d = Dog.new
d.person.build # => nil object on nil.build

I have also tried

d = Dog.new
d.person = Person.new
d.save # => true

The foreign key in Dog is not set in this case due to the fact that at the time it is saved, the new person does not have an id because it has not been saved yet.

My questions are:

  1. How does build work so that Rails is smart enough to figure out how to save the records in the right order?

  2. How can I do the same thing in a belongs_to relationship?

  3. Where can I find any documentation on this method?

Thank you

  • 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-05-11T18:23:38+00:00Added an answer on May 11, 2026 at 6:23 pm

    Where it is documented:

    From the API documentation under the has_many association in “Module ActiveRecord::Associations::ClassMethods“

    collection.build(attributes = {}, …)
    Returns one or more new objects of the
    collection type that have been
    instantiated with attributes and
    linked to this object through a
    foreign key, but have not yet been
    saved. Note: This only works if an
    associated object already exists, not
    if it‘s nil!

    The answer to building in the opposite direction is a slightly altered syntax. In your example with the dogs,

    Class Dog
       has_many :tags
       belongs_to :person
    end
    
    Class Person
      has_many :dogs
    end
    
    d = Dog.new
    d.build_person(:attributes => "go", :here => "like normal")
    

    or even

    t = Tag.new
    t.build_dog(:name => "Rover", :breed => "Maltese")
    

    You can also use create_dog to have it saved instantly (much like the corresponding “create” method you can call on the collection)

    How is rails smart enough? It’s magic (or more accurately, I just don’t know, would love to find out!)

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

Sidebar

Related Questions

I have been unable to find any documentation on properly closing database connections in
I have been unable to find any decent documentation on this function. The code
After an extensive search I have been unable to find any information on this
I have been searching but unable to find a proper list. Can someone please
Does anyone have experience with Sparse ? I seem unable to find any documentation,
I have been unable to locate any documentation for parse! , a very commonly
This is probably a simple question, but i have been unable to find a
I think it might have been asked before but i was unable to find
I have been unable to locate any information on compatibility with Visual Studio 2003
I have only recently started using CakePHP and have been unable to get validation

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.