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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:21:54+00:00 2026-05-16T07:21:54+00:00

When working with activerecord I notice that while constructing associations, the reciprocal association is

  • 0

When working with activerecord I notice that while constructing associations, the reciprocal association is provided automatically only if the reciprocal model has already been saved. If it has not yet been saved, this reciprocal association must be defined manually. I am wondering if this is intentional or a bug, since the only difference in it working or failing is whether the reciprocal model has been saved or not.

I hopefully clarified this with the three scenarios described in the following gist.

http://gist.github.com/527046

I would expect Scenario 1 to work without the extra assignment step shown in Scenario 3. It doesn’t.

  • 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-16T07:21:55+00:00Added an answer on May 16, 2026 at 7:21 am

    The reason is fairly simple – when you create an object through an association where the parent object is not yet saved, its associated foreign key can’t have a value, because the associated record isn’t saved yet. By means of example:

    # Registration announcement is not saved at this point, so it has no ID.
    @a1 = @e1.build_registration_announcement(:announcement => "Please bring a favorite baked dish.")
    # Registration is built without a registration_announcement_id, as @a1 has no ID to pass on.
    @a1.registrations.build(:contact_email => "jdoe@yahoo.com", :adults => 1)
    #registration doesn't know about its unsaved registration_announcement parent
    
    # Registration announcement is created and saved. ID exists.
    @a2 = @e2.create_registration_announcement(:announcement => "Fall house cleaning!")
    # Since the Registration announcement has an ID, registration_announcement_id is set on the built associated registration
    @a2.registrations.build(:contact_email => "tara.smith@yahoo.com", :adults => 5)
    #registration knows about its saved registration_announcement parent
    

    It’s a little unexpected, since you might logically expect Rails to keep the object association around and then assign the association foreign key at save-time, but it doesn’t seem to work that way.

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

Sidebar

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.