<?xml version="1.0" encoding="UTF-8"?>
<ticket>
  <assigned-user-id type="integer">424</assigned-user-id>
  <attachments-count type="integer">2</attachments-count>
  <closed type="boolean">true</closed>
  <created-at type="datetime">2008-04-16T17:54:32+01:00</created-at>
  <creator-id type="integer">5965</creator-id>
  <milestone-due-on type="datetime" nil="true"></milestone-due-on>
  <milestone-id type="integer" nil="true"></milestone-id>
  <number type="integer">9</number>
  <permalink>adding-records-to-a-has_many-collection-does-not-work-on-initialization</permalink>
  <priority type="integer">10897</priority>
  <project-id type="integer">8994</project-id>
  <raw-data type="binary" nil="true" encoding="base64"></raw-data>
  <state>resolved</state>
  <tag>activerecord patch</tag>
  <title>Adding records to a has_many collection does not work on initialization</title>
  <updated-at type="datetime">2008-05-08T06:07:14+01:00</updated-at>
  <user-id type="integer">17393</user-id>
  <user-name>Repository</user-name>
  <creator-name>Andreas Neuhaus</creator-name>
  <assigned-user-name>Joshua Peek</assigned-user-name>
  <url>http://rails.lighthouseapp.com/projects/8994/tickets/9</url>
  <original-body>(from Trac http://dev.rubyonrails.org/ticket/9577. As far as I know, this is still an issue)

@@@ ruby
class User &lt; AR:Base
  has_many :email_addresses
  def email= (address)
    email_addresses.build(:address =&gt; address)
  end
end

class EmailAddress &lt; AR:Base
  belongs_to :user
end
@@@

I use User#email= to create an initial email address record for each user created. It works fine, except if you access User#email_addresses on non-saved user records:

@@@ ruby
# Works fine (creates user record and associated email address record):
User.create!(:username=&gt;'foo', :email=&gt;'foo@bar.baz')

# Also works fine:
u = User.new(:username=&gt;'foo', :email=&gt;'foo@bar.baz')
u.save!

# Does NOT work (user record saved, email address record lost):
u = User.new(:username=&gt;'foo', :email=&gt;'foo@bar.baz')
u.email_addresses   # =&gt; []
u.save!
@@@

Is this a bug or intended behaviour? It looks like the has_many collection cache does not recognize associated objects that are build during initialization.

minaguib suggested a temp workaround:

@@@ ruby
  def email= (address)
    email_addresses(true) unless email_addresses.loaded? # Temp fix for for rails bug #9577
    email_addresses.build(:address =&gt; address)
  end
@@@

Some more info in my &quot;blog&quot;:http://zargony.com/2007/09/17/vanishing-records-on-creating-multiple-models-in-one-action</original-body>
  <latest-body>(from Trac http://dev.rubyonrails.org/ticket/9577. As far as I know, this is still an issue)

@@@ ruby
class User &lt; AR:Base
  has_many :email_addresses
  def email= (address)
    email_addresses.build(:address =&gt; address)
  end
end

class EmailAddress &lt; AR:Base
  belongs_to :user
end
@@@

I use User#email= to create an initial email address record for each user created. It works fine, except if you access User#email_addresses on non-saved user records:

@@@ ruby
# Works fine (creates user record and associated email address record):
User.create!(:username=&gt;'foo', :email=&gt;'foo@bar.baz')

# Also works fine:
u = User.new(:username=&gt;'foo', :email=&gt;'foo@bar.baz')
u.save!

# Does NOT work (user record saved, email address record lost):
u = User.new(:username=&gt;'foo', :email=&gt;'foo@bar.baz')
u.email_addresses   # =&gt; []
u.save!
@@@

Is this a bug or intended behaviour? It looks like the has_many collection cache does not recognize associated objects that are build during initialization.

minaguib suggested a temp workaround:

@@@ ruby
  def email= (address)
    email_addresses(true) unless email_addresses.loaded? # Temp fix for for rails bug #9577
    email_addresses.build(:address =&gt; address)
  end
@@@

Some more info in my &quot;blog&quot;:http://zargony.com/2007/09/17/vanishing-records-on-creating-multiple-models-in-one-action</latest-body>
  <original-body-html>&lt;div&gt;&lt;p&gt;
(from Trac &lt;a href=&quot;http://dev.rubyonrails.org/ticket/9577&quot;&gt;http://dev.rubyonrails.org/ticke...&lt;/a&gt;. As far as I know, this is still an issue)
&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;class User &amp;lt; AR:Base
  has_many :email_addresses
  def email= (address)
    email_addresses.build(:address =&amp;gt; address)
  end
end

class EmailAddress &amp;lt; AR:Base
  belongs_to :user
end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
I use User#email= to create an initial email address record for each user created. It works fine, except if you access User#email_addresses on non-saved user records:
&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;# Works fine (creates user record and associated email address record):
User.create!(:username=&amp;gt;'foo', :email=&amp;gt;'&lt;a href=&quot;mailto:foo@bar.baz&quot;&gt;foo@bar.baz&lt;/a&gt;')

# Also works fine:
u = User.new(:username=&amp;gt;'foo', :email=&amp;gt;'&lt;a href=&quot;mailto:foo@bar.baz&quot;&gt;foo@bar.baz&lt;/a&gt;')
u.save!

# Does NOT work (user record saved, email address record lost):
u = User.new(:username=&amp;gt;'foo', :email=&amp;gt;'&lt;a href=&quot;mailto:foo@bar.baz&quot;&gt;foo@bar.baz&lt;/a&gt;')
u.email_addresses   # =&amp;gt; []
u.save!&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Is this a bug or intended behaviour? It looks like the has_many collection cache does not recognize associated objects that are build during initialization.
&lt;/p&gt;&lt;p&gt;
minaguib suggested a temp workaround:
&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;  def email= (address)
    email_addresses(true) unless email_addresses.loaded? # Temp fix for for rails bug &lt;a href=&quot;/projects/8994/tickets/9577&quot; title=&quot;Ticket #9577&quot;&gt;#9577&lt;/a&gt;
    email_addresses.build(:address =&amp;gt; address)
  end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Some more info in my &lt;a href=&quot;http://zargony.com/2007/09/17/vanishing-records-on-creating-multiple-models-in-one-action&quot;&gt;blog&lt;/a&gt;
&lt;/p&gt;&lt;/div&gt;</original-body-html>
  <versions type="array">
    <version type="Ticket::Version">
      <assigned-user-id type="integer" nil="true"></assigned-user-id>
      <attachments-count type="integer">0</attachments-count>
      <body>(from Trac http://dev.rubyonrails.org/ticket/9577. As far as I know, this is still an issue)

@@@ ruby
class User &lt; AR:Base
  has_many :email_addresses
  def email= (address)
    email_addresses.build(:address =&gt; address)
  end
end

class EmailAddress &lt; AR:Base
  belongs_to :user
end
@@@

I use User#email= to create an initial email address record for each user created. It works fine, except if you access User#email_addresses on non-saved user records:

@@@ ruby
# Works fine (creates user record and associated email address record):
User.create!(:username=&gt;'foo', :email=&gt;'foo@bar.baz')

# Also works fine:
u = User.new(:username=&gt;'foo', :email=&gt;'foo@bar.baz')
u.save!

# Does NOT work (user record saved, email address record lost):
u = User.new(:username=&gt;'foo', :email=&gt;'foo@bar.baz')
u.email_addresses   # =&gt; []
u.save!
@@@

Is this a bug or intended behaviour? It looks like the has_many collection cache does not recognize associated objects that are build during initialization.

minaguib suggested a temp workaround:

@@@ ruby
  def email= (address)
    email_addresses(true) unless email_addresses.loaded? # Temp fix for for rails bug #9577
    email_addresses.build(:address =&gt; address)
  end
@@@

Some more info in my &quot;blog&quot;:http://zargony.com/2007/09/17/vanishing-records-on-creating-multiple-models-in-one-action</body>
      <body-html>&lt;div&gt;&lt;p&gt;
(from Trac &lt;a href=&quot;http://dev.rubyonrails.org/ticket/9577&quot;&gt;http://dev.rubyonrails.org/ticke...&lt;/a&gt;. As far as I know, this is still an issue)
&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;class User &amp;lt; AR:Base
  has_many :email_addresses
  def email= (address)
    email_addresses.build(:address =&amp;gt; address)
  end
end

class EmailAddress &amp;lt; AR:Base
  belongs_to :user
end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
I use User#email= to create an initial email address record for each user created. It works fine, except if you access User#email_addresses on non-saved user records:
&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;# Works fine (creates user record and associated email address record):
User.create!(:username=&amp;gt;'foo', :email=&amp;gt;'&lt;a href=&quot;mailto:foo@bar.baz&quot;&gt;foo@bar.baz&lt;/a&gt;')

# Also works fine:
u = User.new(:username=&amp;gt;'foo', :email=&amp;gt;'&lt;a href=&quot;mailto:foo@bar.baz&quot;&gt;foo@bar.baz&lt;/a&gt;')
u.save!

# Does NOT work (user record saved, email address record lost):
u = User.new(:username=&amp;gt;'foo', :email=&amp;gt;'&lt;a href=&quot;mailto:foo@bar.baz&quot;&gt;foo@bar.baz&lt;/a&gt;')
u.email_addresses   # =&amp;gt; []
u.save!&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Is this a bug or intended behaviour? It looks like the has_many collection cache does not recognize associated objects that are build during initialization.
&lt;/p&gt;&lt;p&gt;
minaguib suggested a temp workaround:
&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;  def email= (address)
    email_addresses(true) unless email_addresses.loaded? # Temp fix for for rails bug &lt;a href=&quot;/projects/8994/tickets/9577&quot; title=&quot;Ticket #9577&quot;&gt;#9577&lt;/a&gt;
    email_addresses.build(:address =&amp;gt; address)
  end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Some more info in my &lt;a href=&quot;http://zargony.com/2007/09/17/vanishing-records-on-creating-multiple-models-in-one-action&quot;&gt;blog&lt;/a&gt;
&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2008-04-16T17:54:32+01:00</created-at>
      <creator-id type="integer">5965</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer" nil="true"></milestone-id>
      <number type="integer">9</number>
      <permalink>adding-records-to-a-has_many-collection-does-not-work-on-initialization</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag nil="true"></tag>
      <title>Adding records to a has_many collection does not work on initialization</title>
      <updated-at type="datetime">2008-04-16T17:54:32+01:00</updated-at>
      <user-id type="integer">5965</user-id>
      <user-name>Andreas Neuhaus</user-name>
      <creator-name>Andreas Neuhaus</creator-name>
      <assigned-user-name nil="true"></assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/9</url>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">424</assigned-user-id>
      <attachments-count type="integer">0</attachments-count>
      <body>I wrote up some ActiveRecord tests for it, however they are passing for me. Did you try this on edge? Or are my test cases not covering the same thing you are referring too?</body>
      <body-html>&lt;div&gt;&lt;p&gt;
I wrote up some ActiveRecord tests for it, however they are passing for me. Did you try this on edge? Or are my test cases not covering the same thing you are referring too?
&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2008-04-16T20:16:03+01:00</created-at>
      <creator-id type="integer">5965</creator-id>
      <diffable-attributes type="yaml">--- 
:assigned_user: 
</diffable-attributes>
      <milestone-id type="integer" nil="true"></milestone-id>
      <number type="integer">9</number>
      <permalink>adding-records-to-a-has_many-collection-does-not-work-on-initialization</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag nil="true"></tag>
      <title>Adding records to a has_many collection does not work on initialization</title>
      <updated-at type="datetime">2008-04-16T20:16:03+01:00</updated-at>
      <user-id type="integer">424</user-id>
      <user-name>Joshua Peek</user-name>
      <creator-name>Andreas Neuhaus</creator-name>
      <assigned-user-name>Joshua Peek</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/9</url>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">424</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body></body>
      <body-html></body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2008-04-16T20:17:00+01:00</created-at>
      <creator-id type="integer">5965</creator-id>
      <diffable-attributes type="yaml">--- 
:state: new
</diffable-attributes>
      <milestone-id type="integer" nil="true"></milestone-id>
      <number type="integer">9</number>
      <permalink>adding-records-to-a-has_many-collection-does-not-work-on-initialization</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag nil="true"></tag>
      <title>Adding records to a has_many collection does not work on initialization</title>
      <updated-at type="datetime">2008-04-16T20:17:00+01:00</updated-at>
      <user-id type="integer">424</user-id>
      <user-name>Joshua Peek</user-name>
      <creator-name>Andreas Neuhaus</creator-name>
      <assigned-user-name>Joshua Peek</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/9</url>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">424</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>I've had a quick poke around with this and I can't seem to get a failing test either. However, I was able to replicate the problem in a Rails app using the exact models and code described by zargony.</body>
      <body-html>&lt;div&gt;&lt;p&gt;
I've had a quick poke around with this and I can't seem to get a failing test either. However, I was able to replicate the problem in a Rails app using the exact models and code described by zargony.
&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2008-04-16T22:43:21+01:00</created-at>
      <creator-id type="integer">5965</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer" nil="true"></milestone-id>
      <number type="integer">9</number>
      <permalink>adding-records-to-a-has_many-collection-does-not-work-on-initialization</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag nil="true"></tag>
      <title>Adding records to a has_many collection does not work on initialization</title>
      <updated-at type="datetime">2008-04-16T22:43:21+01:00</updated-at>
      <user-id type="integer">993</user-id>
      <user-name>Simon Jefford</user-name>
      <creator-name>Andreas Neuhaus</creator-name>
      <assigned-user-name>Joshua Peek</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/9</url>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">424</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>I can only get this to happen inside script/console.

Again, using the models described by Andreas, I ran the following using script/runner:

@@@ ruby
u = User.new(:username =&gt; 'foo', :email =&gt; 'foo@bar.baz')
u.email_addresses
u.save!
puts u.email_addresses.first.address
@@@

and got

foo@bar.baz.</body>
      <body-html>&lt;div&gt;&lt;p&gt;
I can only get this to happen inside script/console.
&lt;/p&gt;&lt;p&gt;
Again, using the models described by Andreas, I ran the following using script/runner:
&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;u = User.new(:username =&amp;gt; 'foo', :email =&amp;gt; '&lt;a href=&quot;mailto:foo@bar.baz&quot;&gt;foo@bar.baz&lt;/a&gt;')
u.email_addresses
u.save!
puts u.email_addresses.first.address&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
and got
&lt;/p&gt;&lt;p&gt;
&lt;a href=&quot;mailto:foo@bar.baz&quot;&gt;foo@bar.baz&lt;/a&gt;.
&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2008-04-17T13:36:12+01:00</created-at>
      <creator-id type="integer">5965</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer" nil="true"></milestone-id>
      <number type="integer">9</number>
      <permalink>adding-records-to-a-has_many-collection-does-not-work-on-initialization</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag nil="true"></tag>
      <title>Adding records to a has_many collection does not work on initialization</title>
      <updated-at type="datetime">2008-04-17T13:36:12+01:00</updated-at>
      <user-id type="integer">993</user-id>
      <user-name>Simon Jefford</user-name>
      <creator-name>Andreas Neuhaus</creator-name>
      <assigned-user-name>Joshua Peek</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/9</url>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">424</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>Interestingly, the test also passes for me. But the bug still occurs when using the console of a project where I disabled the described workaround.

Though I don't see why, there must be something wrong with the test. I'll do some more tests later today.</body>
      <body-html>&lt;div&gt;&lt;p&gt;
Interestingly, the test also passes for me. But the bug still occurs when using the console of a project where I disabled the described workaround.
&lt;/p&gt;&lt;p&gt;
Though I don't see why, there must be something wrong with the test. I'll do some more tests later today.
&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2008-04-17T14:37:50+01:00</created-at>
      <creator-id type="integer">5965</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer" nil="true"></milestone-id>
      <number type="integer">9</number>
      <permalink>adding-records-to-a-has_many-collection-does-not-work-on-initialization</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag nil="true"></tag>
      <title>Adding records to a has_many collection does not work on initialization</title>
      <updated-at type="datetime">2008-04-17T14:37:50+01:00</updated-at>
      <user-id type="integer">5965</user-id>
      <user-name>Andreas Neuhaus</user-name>
      <creator-name>Andreas Neuhaus</creator-name>
      <assigned-user-name>Joshua Peek</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/9</url>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">424</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>Joshua: yes, I'm trying with rails edge pulled from git.

The problem occurs, if you create an object of a model which builds associated objects during initialization. If you access these associated objects before saving the object itself, all associated objects are lost.

I guess, that this problem might be related to the lazy-loading of an associated collection. If an object builds associated objects in its initializer, the association is still marked as loaded?==false; so on first access to the association proxy, it'll try to load the collection and wipes the ones built in memory.

For some reason, it looks like accessing email_addresses.first is ok, but accessing email_addresses or doing email_addresses.inspect results in an empty array:

@@@
&gt;&gt; u=User.new(:username=&gt;'foo', :email=&gt;'foo@bar.baz')
=&gt; #&lt;User id: nil, username: &quot;foo&quot;, created_at: nil, updated_at: nil&gt;
&gt;&gt; u.email_addresses.loaded?
=&gt; false
&gt;&gt; u.email_addresses.first
=&gt; #&lt;EmailAddress id: nil, address: &quot;foo@bar.baz&quot;, user_id: nil, created_at: nil, updated_at: nil&gt;
&gt;&gt; u.email_addresses.loaded?
=&gt; true
&gt;&gt; u.email_addresses
=&gt; [#&lt;EmailAddress id: nil, address: &quot;foo@bar.baz&quot;, user_id: nil, created_at: nil, updated_at: nil&gt;]
&gt;&gt;
?&gt; u=User.new(:username=&gt;'foo', :email=&gt;'foo@bar.baz')
=&gt; #&lt;User id: nil, username: &quot;foo&quot;, created_at: nil, updated_at: nil&gt;
&gt;&gt; u.email_addresses.loaded?
=&gt; false
&gt;&gt; u.email_addresses
=&gt; []
&gt;&gt; u.email_addresses.loaded?
=&gt; true
@@@

I can reproduce this with script/console and with script/runner, but for some reason not with a test...</body>
      <body-html>&lt;div&gt;&lt;p&gt;
Joshua: yes, I'm trying with rails edge pulled from git.
&lt;/p&gt;&lt;p&gt;
The problem occurs, if you create an object of a model which builds associated objects during initialization. If you access these associated objects before saving the object itself, all associated objects are lost.
&lt;/p&gt;&lt;p&gt;
I guess, that this problem might be related to the lazy-loading of an associated collection. If an object builds associated objects in its initializer, the association is still marked as loaded?==false; so on first access to the association proxy, it'll try to load the collection and wipes the ones built in memory.
&lt;/p&gt;&lt;p&gt;
For some reason, it looks like accessing email_addresses.first is ok, but accessing email_addresses or doing email_addresses.inspect results in an empty array:
&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;&quot;&gt;&amp;gt;&amp;gt; u=User.new(:username=&amp;gt;'foo', :email=&amp;gt;'&lt;a href=&quot;mailto:foo@bar.baz&quot;&gt;foo@bar.baz&lt;/a&gt;')
=&amp;gt; #&amp;lt;User id: nil, username: &amp;quot;foo&amp;quot;, created_at: nil, updated_at: nil&amp;gt;
&amp;gt;&amp;gt; u.email_addresses.loaded?
=&amp;gt; false
&amp;gt;&amp;gt; u.email_addresses.first
=&amp;gt; #&amp;lt;EmailAddress id: nil, address: &amp;quot;&lt;a href=&quot;mailto:foo@bar.baz&quot;&gt;foo@bar.baz&lt;/a&gt;&amp;quot;, user_id: nil, created_at: nil, updated_at: nil&amp;gt;
&amp;gt;&amp;gt; u.email_addresses.loaded?
=&amp;gt; true
&amp;gt;&amp;gt; u.email_addresses
=&amp;gt; [#&amp;lt;EmailAddress id: nil, address: &amp;quot;&lt;a href=&quot;mailto:foo@bar.baz&quot;&gt;foo@bar.baz&lt;/a&gt;&amp;quot;, user_id: nil, created_at: nil, updated_at: nil&amp;gt;]
&amp;gt;&amp;gt;
?&amp;gt; u=User.new(:username=&amp;gt;'foo', :email=&amp;gt;'&lt;a href=&quot;mailto:foo@bar.baz&quot;&gt;foo@bar.baz&lt;/a&gt;')
=&amp;gt; #&amp;lt;User id: nil, username: &amp;quot;foo&amp;quot;, created_at: nil, updated_at: nil&amp;gt;
&amp;gt;&amp;gt; u.email_addresses.loaded?
=&amp;gt; false
&amp;gt;&amp;gt; u.email_addresses
=&amp;gt; []
&amp;gt;&amp;gt; u.email_addresses.loaded?
=&amp;gt; true&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
I can reproduce this with script/console and with script/runner, but for some reason not with a test...
&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2008-04-17T15:33:59+01:00</created-at>
      <creator-id type="integer">5965</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer" nil="true"></milestone-id>
      <number type="integer">9</number>
      <permalink>adding-records-to-a-has_many-collection-does-not-work-on-initialization</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag nil="true"></tag>
      <title>Adding records to a has_many collection does not work on initialization</title>
      <updated-at type="datetime">2008-04-17T15:33:59+01:00</updated-at>
      <user-id type="integer">5965</user-id>
      <user-name>Andreas Neuhaus</user-name>
      <creator-name>Andreas Neuhaus</creator-name>
      <assigned-user-name>Joshua Peek</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/9</url>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">424</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>Please reopen this ticket once you've got some tests.</body>
      <body-html>&lt;div&gt;&lt;p&gt;
Please reopen this ticket once you've got some tests.
&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">true</closed>
      <created-at type="datetime">2008-04-19T22:38:36+01:00</created-at>
      <creator-id type="integer">5965</creator-id>
      <diffable-attributes type="yaml">--- 
:state: open
</diffable-attributes>
      <milestone-id type="integer" nil="true"></milestone-id>
      <number type="integer">9</number>
      <permalink>adding-records-to-a-has_many-collection-does-not-work-on-initialization</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>hold</state>
      <tag nil="true"></tag>
      <title>Adding records to a has_many collection does not work on initialization</title>
      <updated-at type="datetime">2008-04-19T22:38:36+01:00</updated-at>
      <user-id type="integer">424</user-id>
      <user-name>Joshua Peek</user-name>
      <creator-name>Andreas Neuhaus</creator-name>
      <assigned-user-name>Joshua Peek</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/9</url>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">424</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>I think I finally tracked it down. It's the inspect method of AssociationProxy that does a reload if the target isn't loaded yet and therefore wipes the collection in this case.

The console automatically does #inspect to display an object. That's why it always happened in the console but not in tests.

I attached a patch that fixes this problem and adds a proper test.

Unfortunately, I cannot reopen this ticket myself.</body>
      <body-html>&lt;div&gt;&lt;p&gt;
I think I finally tracked it down. It's the inspect method of AssociationProxy that does a reload if the target isn't loaded yet and therefore wipes the collection in this case.
&lt;/p&gt;&lt;p&gt;
The console automatically does #inspect to display an object. That's why it always happened in the console but not in tests.
&lt;/p&gt;&lt;p&gt;
I attached a patch that fixes this problem and adds a proper test.
&lt;/p&gt;&lt;p&gt;
Unfortunately, I cannot reopen this ticket myself.
&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">true</closed>
      <created-at type="datetime">2008-04-21T13:54:48+01:00</created-at>
      <creator-id type="integer">5965</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer" nil="true"></milestone-id>
      <number type="integer">9</number>
      <permalink>adding-records-to-a-has_many-collection-does-not-work-on-initialization</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>hold</state>
      <tag nil="true"></tag>
      <title>Adding records to a has_many collection does not work on initialization</title>
      <updated-at type="datetime">2008-04-21T13:54:48+01:00</updated-at>
      <user-id type="integer">5965</user-id>
      <user-name>Andreas Neuhaus</user-name>
      <creator-name>Andreas Neuhaus</creator-name>
      <assigned-user-name>Joshua Peek</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/9</url>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">424</assigned-user-id>
      <attachments-count type="integer">2</attachments-count>
      <body></body>
      <body-html></body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2008-05-02T23:36:09+01:00</created-at>
      <creator-id type="integer">5965</creator-id>
      <diffable-attributes type="yaml">--- 
:state: hold
</diffable-attributes>
      <milestone-id type="integer" nil="true"></milestone-id>
      <number type="integer">9</number>
      <permalink>adding-records-to-a-has_many-collection-does-not-work-on-initialization</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>open</state>
      <tag nil="true"></tag>
      <title>Adding records to a has_many collection does not work on initialization</title>
      <updated-at type="datetime">2008-05-02T23:36:09+01:00</updated-at>
      <user-id type="integer">141</user-id>
      <user-name>Michael Koziarski</user-name>
      <creator-name>Andreas Neuhaus</creator-name>
      <assigned-user-name>Joshua Peek</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/9</url>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">424</assigned-user-id>
      <attachments-count type="integer">2</attachments-count>
      <body>(from [bcb090c56b842a76397e0ea32f54c942fd11910e]) Calling ActiveRecord#inspect on an unloaded association won't wipe the collection [#9 state:resolved]
Signed-off-by: Joshua Peek &lt;josh@joshpeek.com&gt;
http://github.com/rails/rails/commit/bcb090c56b842a76397e0ea32f54c942fd11910e</body>
      <body-html>&lt;div&gt;&lt;p&gt;
(from [bcb090c56b842a76397e0ea32f54c942fd11910e]) Calling ActiveRecord#inspect on an unloaded association won't wipe the collection [#9 state:resolved]
&lt;/p&gt;&lt;p&gt;
Signed-off-by: Joshua Peek 
&lt;/p&gt;&lt;p&gt;
&lt;a href=&quot;http://github.com/rails/rails/commit/bcb090c56b842a76397e0ea32f54c942fd11910e&quot;&gt;http://github.com/rails/rails/co...&lt;/a&gt;
&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">true</closed>
      <created-at type="datetime">2008-05-08T06:07:14+01:00</created-at>
      <creator-id type="integer">5965</creator-id>
      <diffable-attributes type="yaml">--- 
:state: open
</diffable-attributes>
      <milestone-id type="integer" nil="true"></milestone-id>
      <number type="integer">9</number>
      <permalink>adding-records-to-a-has_many-collection-does-not-work-on-initialization</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>resolved</state>
      <tag nil="true"></tag>
      <title>Adding records to a has_many collection does not work on initialization</title>
      <updated-at type="datetime">2008-05-08T06:07:14+01:00</updated-at>
      <user-id type="integer">17393</user-id>
      <user-name>Repository</user-name>
      <creator-name>Andreas Neuhaus</creator-name>
      <assigned-user-name>Joshua Peek</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/9</url>
    </version>
  </versions>
  <attachments type="array">
    <attachment type="Attachment">
      <code>bf40dfc5f9f44991b0579088fb6587e7a954b6b6</code>
      <content-type>text/plain</content-type>
      <created-at type="datetime">2008-04-16T20:16:03+01:00</created-at>
      <filename>has_many_initialization_tests.diff</filename>
      <height type="integer" nil="true"></height>
      <id type="integer">18881</id>
      <size type="integer">1397</size>
      <uploader-id type="integer">424</uploader-id>
      <width type="integer" nil="true"></width>
      <url>http://rails.lighthouseapp.com/attachments/18881/has_many_initialization_tests.diff</url>
    </attachment>
    <attachment type="Attachment">
      <code>0fd409a1dd86b33ad68c2a71245e1d0dbc404230</code>
      <content-type>text/plain</content-type>
      <created-at type="datetime">2008-04-21T13:54:48+01:00</created-at>
      <filename>0001-Inspect-on-a-not-yet-loaded-association-does-no-long.patch</filename>
      <height type="integer" nil="true"></height>
      <id type="integer">19455</id>
      <size type="integer">2273</size>
      <uploader-id type="integer">5965</uploader-id>
      <width type="integer" nil="true"></width>
      <url>http://rails.lighthouseapp.com/attachments/19455/0001-Inspect-on-a-not-yet-loaded-association-does-no-long.patch</url>
    </attachment>
  </attachments>
</ticket>
