# Building associations

**URL:** <https://www.ruby-forum.com/t/building-associations/112752>\
**Category:** Rails\
**Created:** [August 22, 2007, 9:44pm UTC](https://www.ruby-forum.com/t/building-associations/112752 "2007-08-22T21:44:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wiliam\_Langshaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/c89c15/32.png) [@Wiliam\_Langshaw](https://www.ruby-forum.com/u/Wiliam_Langshaw)\
**Post date:** [August 22, 2007, 9:44pm UTC](https://www.ruby-forum.com/t/building-associations/112752/1 "2007-08-22T21:44:16Z")

</div>

I have a model with has\_many (not :through) and am adding members to  
it like this:

class Panda \< AR::Base  
has\_many :baby\_bears, :after\_add =\> :increment\_baby\_count

def do\_it  
self.baby\_bears \<\< BabyBear.create({:name =\> “Fluffy”})  
end  
end

In looking at the log, this results in 2 SQL statements, an INSERT  
(for the create) immediately followed by an UPDATE of the same record  
(no changes were made!).

If I look in vendor/rails/active\_record/lib/active\_record/associations/  
has\_many\_associations.rb#insert\_record

which handles the over-loading of the “\<\<” method I can see that it is  
blindly calling “record.save”, which is the cause of the UPDATE. In  
this case its the perfect situation for ActiveRecord supporting a  
“dirty” bit.

However, I think that I am not doing this correctly. That is, I am not  
adding my association member correctly.

If I use “self.baby\_bears.create({:name =\> “Fluffy”})” then  
my :after\_add callback does NOT get called, but it does get called in  
the first method.

In summary:

How do I add an association member that does not generate 2 SQL  
statements AND get my :after\_add callback executed?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Wiliam\_Langshaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/c89c15/32.png) [@Wiliam\_Langshaw](https://www.ruby-forum.com/u/Wiliam_Langshaw)\
**Post date:** [August 23, 2007, 3:29pm UTC](https://www.ruby-forum.com/t/building-associations/112752/2 "2007-08-23T15:29:39Z")

</div>

Wiliam Langshaw wrote:

> How do I add an association member that does not generate 2 SQL  
> statements AND get my :after\_add callback executed?

What about

```
self.baby_bears << BabyBear.new(:name => "Fluffy")

```

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![Wiliam\_Langshaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/c89c15/32.png) [@Wiliam\_Langshaw](https://www.ruby-forum.com/u/Wiliam_Langshaw)\
**Post date:** [August 23, 2007, 3:54pm UTC](https://www.ruby-forum.com/t/building-associations/112752/3 "2007-08-23T15:54:44Z")

</div>

or simply

self.baby\_bears.create(:name =\> “Fluffy”)

---

<div class="post-metadata">

**Author:** ![Wiliam\_Langshaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/c89c15/32.png) [@Wiliam\_Langshaw](https://www.ruby-forum.com/u/Wiliam_Langshaw)\
**Post date:** [August 23, 2007, 9:21pm UTC](https://www.ruby-forum.com/t/building-associations/112752/4 "2007-08-23T21:21:50Z")

</div>

Yup, thats the ticket, using “new” versus “create”. I should have  
thought of that before, I feel like an idiot!

@Thorsten: your idea of

self.baby\_bears.create(:name =\> “Fluffy”)

does _not_ (completely) work because the :after\_add callback is not  
being triggered.

Thanks!
