ActiveRecord: Many-to-Many problem


#1

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?


#2

On 4/21/06, zven 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


#3

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


#4

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


#5

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.