Note that there are some explanatory texts on larger screens.

plurals
  1. PORails ActiveRecord associations inconsistently updated
    primarykey
    data
    text
    <p>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. </p> <p>This is best explained with an example:</p> <p>Create a <code>Post</code> model with the string attribute <code>'title'</code> and a <code>Comment</code> model with the string attribute <code>'content'</code>. </p> <p>Here are the associations: </p> <pre><code>class Post &lt; ActiveRecord::Base has_many :comments end class Comment &lt; ActiveRecord::Base belongs_to :post end </code></pre> <p>Scenario #1: In the following code I create one <code>Post</code> with an associated <code>Comment</code>, create a second <code>Post</code> by <code>find</code>'ing the first, add a second <code>Comment</code> to the first <code>Post</code> and discover that the second <code>Post</code> has the second <code>Comment</code> associated to it without an explicit assignment.</p> <pre><code>post1 = Post.new post1 = Post.new(:title =&gt; 'Post 1') comment1 = Comment.new(:content =&gt; 'content 1') post1.comments &lt;&lt; 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 =&gt; 'content 2') post1.comments &lt;&lt; comment2 # Note that both Comments are associated with both Post objects even # though I never explicitly associated it with post2. post1.comment_ids # =&gt; [12, 13] post2.comment_ids # =&gt; [12, 13] </code></pre> <p>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 <code>post2.comments</code> which occurs <em>after</em> creating <code>comment2</code> and <em>before</em> adding <code>comment2</code> to <code>post1</code>. </p> <pre><code>post1 = Post.new post1 = Post.new(:title =&gt; 'Post 1A') comment1 = Comment.new(:content =&gt; 'content 1A') post1.comments &lt;&lt; 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 =&gt; 'content 2A') post2.comments # !! THIS IS THE EXTRA COMMAND !! post1.comments &lt;&lt; comment2 # Note that both Comments are associated with both Post objects even # though I never explicitly associated it with post2. post1.comment_ids # =&gt; [14, 15] post2.comment_ids # =&gt; [14] </code></pre> <p>Note that there is only <strong>one</strong> comment associated with <code>post2</code> in this scenario whereas in Scenario 1 there were two. </p> <p>The Big Question: Why would running <code>post2.comments</code> before adding the new <code>Comment</code> to <code>post1</code> make any difference to which Comments were associated with <code>post2</code>? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload