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

  • Home
  • SEARCH
  • 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 620715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:47:39+00:00 2026-05-13T18:47:39+00:00

I am running into some Rails 2.3.5 ActiveRecord behavior I do not understand. It

  • 0

I am running into some Rails 2.3.5 ActiveRecord behavior I do not understand. It appears that an object can have its association ids updated in inconsistent ways.

This is best explained with an example:

Create a Post model with the string attribute 'title' and a Comment model with the string attribute 'content'.

Here are the associations:

class Post < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :post
end

Scenario #1: In the following code I create one Post with an associated Comment, create a second Post by find‘ing the first, add a second Comment to the first Post and discover that the second Post has the second Comment associated to it without an explicit assignment.

post1 = Post.new
post1 = Post.new(:title => 'Post 1')
comment1 = Comment.new(:content => 'content 1')
post1.comments << comment1
post1.save
# Create a second Post object by find'ing the first
post2 = Post.find_by_title('Post 1')
# Add a new Comment to the first Post object
comment2 = Comment.new(:content => 'content 2')
post1.comments << comment2 
# Note that both Comments are associated with both Post objects even
# though I never explicitly associated it with post2.
post1.comment_ids # => [12, 13]
post2.comment_ids # => [12, 13]

Scenario #2: Run the above commands again but this time insert one extra command that, on the face of it, should not affect the results. The extra command is post2.comments which occurs after creating comment2 and before adding comment2 to post1.

post1 = Post.new
post1 = Post.new(:title => 'Post 1A')
comment1 = Comment.new(:content => 'content 1A')
post1.comments << comment1
post1.save
# Create a second Post object by find'ing the first
post2 = Post.find_by_title('Post 1A')
# Add a new Comment to the first Post object
comment2 = Comment.new(:content => 'content 2A')
post2.comments # !! THIS IS THE EXTRA COMMAND !!
post1.comments << comment2 
# Note that both Comments are associated with both Post objects even
# though I never explicitly associated it with post2.
post1.comment_ids # => [14, 15]
post2.comment_ids # => [14]

Note that there is only one comment associated with post2 in this scenario whereas in Scenario 1 there were two.

The Big Question: Why would running post2.comments before adding the new Comment to post1 make any difference to which Comments were associated with post2?

  • 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-13T18:47:39+00:00Added an answer on May 13, 2026 at 6:47 pm

    This has to do with the way that Active Record caches requests and the way that has_many associations are handled.

    Unless the association is eagerloaded with an :include option during the find. Rails will not populate the association for the found records until needed. When the association is needed some memoization is done to cut down on the number of SQL queries executed.

    Stepping through the code in the question:

    post1 = Post.new(:title => 'Post 1')
    comment1 = Comment.new(:content => 'content 1')
    post1.comments << comment1  # updates post1's internal comments cache
    post1.save 
    
    # Create a second Post object by find'ing the first
    post2 = Post.find_by_title('Post 1') 
    
    # Add a new Comment to the first Post object
    comment2 = Comment.new(:content => 'content 2')
    post1.comments << comment2   # updates post1's internal comments cache
    
    # Note that both Comments are associated with both Post objects even
    # though I never explicitly associated it with post2.
    post1.comment_ids # => [12, 13]
    
    # this is the first time post2.comments are loaded. 
    # SELECT comments.* FROM comments JOIN comments.post_id = posts.id WHERE posts.id = #{post2.id}
    post2.comment_ids # => [12, 13]
    

    Scenario 2:

    post1 = Post.new(:title => 'Post 1A')
    comment1 = Comment.new(:content => 'content 1A')
    post1.comments << comment1
    post1.save
    
    # Create a second Post object by find'ing the first
    post2 = Post.find_by_title('Post 1A')
    
    # Add a new Comment to the first Post object
    comment2 = Comment.new(:content => 'content 2A')
    
    # first time post2.comments are loaded. 
    # SELECT comments.* FROM comments JOIN comments.post_id = posts.id WHERE 
    #   posts.id = post2.comments #=> Returns one comment (id = 14)
    # cached internally.
    
    post1.comments << comment2 
    # Note that both Comments are associated with both Post objects even
    # though I never explicitly associated it with post2.
    post1.comment_ids # => [14, 15]
    
    # post2.comment has already been cached, so the SQL query is not executed again.
    
    post2.comment_ids # => [14]
    

    N.B. post2.comment_ids is internally defined as post2.comments.map(&:id)

    P.S. My answer to this question might help you understand why post2 gets updated despite your not touching it.

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

Sidebar

Ask A Question

Stats

  • Questions 340k
  • Answers 340k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I dont think that this can be done the way… May 14, 2026 at 4:49 am
  • Editorial Team
    Editorial Team added an answer You are probably redefining the default parameter in the implementation… May 14, 2026 at 4:49 am
  • Editorial Team
    Editorial Team added an answer I just tried it and it seems to work -… May 14, 2026 at 4:49 am

Related Questions

I need to create a photo gallery for a website running IIS 4.0 or
I am trying to set up Ruby on Rails on windows. I am using
Hey guys. I'm creating a rails app and being one of the first times
After doing some stuff in Ruby on Rails with Cucumber, RSpec and Ruby BDD
I am working on a website hosted on microsoft's office live service. It has

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.