# ActiveRecord: Many-to-Many problem

**URL:** <https://www.ruby-forum.com/t/activerecord-many-to-many-problem/58367>\
**Category:** Rails\
**Created:** [April 20, 2006, 11:28pm UTC](https://www.ruby-forum.com/t/activerecord-many-to-many-problem/58367 "2006-04-20T23:28:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/77aa72/32.png) [@zven](https://www.ruby-forum.com/u/zven)\
**Post date:** [April 20, 2006, 11:28pm UTC](https://www.ruby-forum.com/t/activerecord-many-to-many-problem/58367/1 "2006-04-20T23:28:10Z")

</div>

I am trying to make many-to-may relationship on classes Section and  
Content.  
Both Content and Section has:  
has\_and\_belongs\_to\_many :contents and has\_and\_belongs\_to\_many :sections

and I have an exception when trying to access model properties:  
“undefined method `add\_contents’ for #Section:0xb745c514”

def add\_content\_entry  
@section = Section.find(params[:section\_id])  
@content = Content.find(params[:content\_id])  
@section.add\_contents(@content)  
@section.name = ‘test’  
@section.update  
redirect\_to :action =\> ‘list’  
end

Where I am wrong?

---

<div class="post-metadata">

**Author:** ![zven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/77aa72/32.png) [@zven](https://www.ruby-forum.com/u/zven)\
**Post date:** [April 20, 2006, 11:34pm UTC](https://www.ruby-forum.com/t/activerecord-many-to-many-problem/58367/2 "2006-04-20T23:34:14Z")

</div>

On 4/21/06, zven [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> and I have an exception when trying to access model properties:  
> “undefined method `add\_contents’ for #Section:0xb745c514”
> 
> @section.add\_contents(@content)

There is no such method of add\_\*… You need to use one of the  
following:  
@section.contents \<\< @content  
@section.contents.push(@content)

Josh

---

<div class="post-metadata">

**Author:** ![zven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/77aa72/32.png) [@zven](https://www.ruby-forum.com/u/zven)\
**Post date:** [April 20, 2006, 11:43pm UTC](https://www.ruby-forum.com/t/activerecord-many-to-many-problem/58367/3 "2006-04-20T23:43:24Z")

</div>

On Apr 21, 2006, at 04:23 AM, zven wrote:

> @section = Section.find(params[:section\_id])  
> @content = Content.find(params[:content\_id])  
> @section.add\_contents(@content)

There is no “add\_” method defined by the  
has\_and\_belongs\_to\_many association. If you want to add a record to  
your contents\_sections join table, all you need to do is:

@section.contents \<\< @content

-Brian

---

<div class="post-metadata">

**Author:** ![zven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/77aa72/32.png) [@zven](https://www.ruby-forum.com/u/zven)\
**Post date:** [April 20, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/activerecord-many-to-many-problem/58367/4 "2006-04-20T23:46:29Z")

</div>

On Thu, 2006-04-20 at 14:31 -0700, Josh K. wrote:

> There is no such method of add\_\*… You need to use one of the  
> following:  
> @section.contents \<\< @content  
> @section.contents.push(@content)
> 
> Josh

Thanks for reply Josh, I still have the same: “undefined method  
`contents’ for #Section:0xb74d7bec”

ruby1.8, rails 1.1.0

---

<div class="post-metadata">

**Author:** ![zven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/77aa72/32.png) [@zven](https://www.ruby-forum.com/u/zven)\
**Post date:** [April 20, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/activerecord-many-to-many-problem/58367/5 "2006-04-20T23:52:32Z")

</div>

On Fri, 2006-04-21 at 03:45 -0500, zven wrote:

> On Thu, 2006-04-20 at 14:31 -0700, Josh K. wrote:  
> Thanks for reply Josh, I still have the same: “undefined method  
> `contents’ for #Section:0xb74d7bec”
> 
> ruby1.8, rails 1.1.0

Problem solved. Thanks.
