# Has\_many and the \<\< operator?

**URL:** <https://www.ruby-forum.com/t/has-many-and-the-operator/119098>\
**Category:** Rails\
**Created:** [November 3, 2007, 3:39am UTC](https://www.ruby-forum.com/t/has-many-and-the-operator/119098 "2007-11-03T03:39:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [November 3, 2007, 3:39am UTC](https://www.ruby-forum.com/t/has-many-and-the-operator/119098/1 "2007-11-03T03:39:11Z")

</div>

In the AWDWR book it mentions that the \<\< operator (or method?) just  
adds the child model the child collection within the parent model.

For some reason when I am using it it doesn’t work as expected.

Here is what I have:  
class Member  
has\_many :tasks  
end

class Task  
belongs\_to :member  
end

describe Member, " linkage to task" do  
before(:each) do  
@member = Member.new(valid\_member\_params)  
end

def valid\_member\_params  
{  
:first\_name =\> “Joe”,  
…  
}  
end

it “should be added through a member” do  
task = Task.new(:content =\> “Do something”)  
@member.tasks \<\< task  
@member.save

```
@ member.should have(1).tasks
@ member.tasks.size == 1
@ member.tasks[0].id > 0 #FAILS here as it never got inserted

```

end  
end

The log is rather odd as it doesn’t show any hits to the database:  
Spec::Rails::DSL::HelperEvalContextController: missing default helper  
path spec/rails/dsl/helper\_eval\_context\_helper  
Spec::Rails::DSL::ViewExampleController: missing default helper path  
spec/rails/dsl/view\_example\_helper  
[4;36;1mSQL (0.000187) [0m [0;1mSET SQL\_AUTO\_IS\_NULL=0 [0m  
[4;35;1mSQL (0.000142) [0m [0mBEGIN [0m  
[4;36;1mMembers Columns (0.006676) [0m [0;1mSHOW FIELDS FROM  
members [0m  
[4;35;1mTask Columns (0.007478) [0m [0mSHOW FIELDS FROM  
tasks [0m  
[4;36;1mSQL (0.000180) [0m [0;1mROLLBACK [0m  
[4;35;1mSQL (0.000134) [0m [0mBEGIN [0m  
[4;36;1mSQL (0.000133) [0m [0;1mROLLBACK [0m

Thanks for the help

(and I have no idea why I am getting the warning of missing the  
default helper path)

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [November 3, 2007, 4:00am UTC](https://www.ruby-forum.com/t/has-many-and-the-operator/119098/2 "2007-11-03T04:00:12Z")

</div>

On Nov 2, 7:38 pm, chris [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Task  
> :first\_name =\> “Joe”,  
> @ member.tasks.size == 1  
> [4;36;1mSQL (0.000187) [0m [0;1mSET SQL\_AUTO\_IS\_NULL=0 [0m
> 
> (and I have no idea why I am getting the warning of missing the  
> default helper path)

i think you’re hitting the limitation that you can’t #\<\< to a parent  
that’s not been saved

[http://alloycode.com/2007/10/11/when-things-get-saved](http://alloycode.com/2007/10/11/when-things-get-saved)  
[http://dev.rubyonrails.org/changeset/7511](http://dev.rubyonrails.org/changeset/7511)

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [November 3, 2007, 4:12am UTC](https://www.ruby-forum.com/t/has-many-and-the-operator/119098/3 "2007-11-03T04:12:28Z")

</div>

That is the way that I had it at first, but after referring back to  
the book (pg 330 at the bottom) it shows the save done after the child  
model is added to the parent.

I have tried again doing what you said and it is still failing:  
…  
@listing.save  
@listing.online\_showings \<\< showing  
@listing.online\_showings[0].id \> 0

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [November 3, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/has-many-and-the-operator/119098/4 "2007-11-03T16:15:41Z")

</div>

Thanks for the help.

I moved the save to after all the child models were added and for some  
reason started getting a validation error. I then clued in that the  
validates\_as\_attachment was failing for the attachment\_fu plugin.  
Once I disabled that it started working as expected.

Thanks again.

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [November 3, 2007, 9:50am UTC](https://www.ruby-forum.com/t/has-many-and-the-operator/119098/5 "2007-11-03T09:50:17Z")

</div>

As a rule of thumb, I would be calling save!, as long as I wasn’t  
checking for errors. Maybe save is failing?

///ark

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [November 3, 2007, 5:44pm UTC](https://www.ruby-forum.com/t/has-many-and-the-operator/119098/6 "2007-11-03T17:44:02Z")

</div>

I am still using 1.2.5, although that would explain the NoMethodError,  
which somehow disappeared.

On Nov 3, 9:28 am, Josh S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [November 3, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/has-many-and-the-operator/119098/7 "2007-11-03T16:28:48Z")

</div>

Chris O. wrote:

> Thanks for the help.
> 
> I moved the save to after all the child models were added and for some  
> reason started getting a validation error. I then clued in that the  
> validates\_as\_attachment was failing for the attachment\_fu plugin.  
> Once I disabled that it started working as expected.

What version of Rails are you using? There was a bug in edge (  
[http://dev.rubyonrails.org/ticket/9989](http://dev.rubyonrails.org/ticket/9989) ) that hit has\_many \<\< that got  
fixed very recently. It prevented \<\< from working on has\_many assocs for  
new objects where the assoc hasn’t been mentioned yet. That was fixed  
last week in [8049].

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
