# Activerecord \<\< operator

**URL:** <https://www.ruby-forum.com/t/activerecord-operator/53686>\
**Category:** Rails\
**Created:** [February 13, 2006, 7:01pm UTC](https://www.ruby-forum.com/t/activerecord-operator/53686 "2006-02-13T19:01:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Mage](https://www.ruby-forum.com/u/Mage)\
**Post date:** [February 13, 2006, 7:01pm UTC](https://www.ruby-forum.com/t/activerecord-operator/53686/1 "2006-02-13T19:01:08Z")

</div>

Hello,

I am new here and actually I am writing my first RoR app.

My question is:

* * *

class Topic \< ActiveRecord::Base  
has\_many :messages  
end

class Message \< ActiveRecord::Base  
belongs\_to :topic  
end

class ForumController \< ApplicationController  
def post  
message = Message.new(params[:message])  
@topic = Topic.find(params[:id])  
@topic.messages \<\< message  
end  
end

Okay, this works. However, if i write

```
    message = Message.new(params[:message])
    @topic = Topic.find(params[:id])
    @topic.messages << message
    @topic.messages << message

```

(last line repeated once), then I got an primary key constraint error  
message.

I understand the database message. I thought this will help:

```
    @topic.messages << message.dup
    @topic.messages << message.dup

```

but it didn’t.

I checked that the class of @topic.messages is Array, and I didn’t find  
any description for the \<\< operator in the rails manual. Does it some  
special, different from Ruby? When is this arra saved (or the new  
element inserted) to the database? How can I append more then one  
element to the end of this ActiveRecord array?

```
   Mage
```

---

<div class="post-metadata">

**Author:** ![Mage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Mage](https://www.ruby-forum.com/u/Mage)\
**Post date:** [February 13, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/activerecord-operator/53686/2 "2006-02-13T19:08:41Z")

</div>

Operator \<\< is the standard Ruby append/push operation

---

<div class="post-metadata">

**Author:** ![Mage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Mage](https://www.ruby-forum.com/u/Mage)\
**Post date:** [February 13, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/activerecord-operator/53686/3 "2006-02-13T19:12:37Z")

</div>

Well, I found that when I write:

active\_record.subitems \<\< subitem

the new subitem will be inserted to the database immediately. Somewhere  
must be a method which does this. But where?

```
   Mage
```

---

<div class="post-metadata">

**Author:** ![Mage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Mage](https://www.ruby-forum.com/u/Mage)\
**Post date:** [February 13, 2006, 7:26pm UTC](https://www.ruby-forum.com/t/activerecord-operator/53686/4 "2006-02-13T19:26:35Z")

</div>

take a look at the has\_many documentation:

[http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000471](http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000471)

also see the section titled “Unsaved objects and associations”  
subheading  
“Collections” toward the top of the page for additional information.
