Forum: Ruby on Rails ActiveRecord: Many-to-Many problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
zven (Guest)
on 2006-04-21 01:28
(Received via mailing list)
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?
Josh K. (Guest)
on 2006-04-21 01:34
(Received via mailing list)
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
Brian V Hughes (Guest)
on 2006-04-21 01:43
(Received via mailing list)
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_<objects>" 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
zven (Guest)
on 2006-04-21 01:46
(Received via mailing list)
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
zven (Guest)
on 2006-04-21 01:52
(Received via mailing list)
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.
This topic is locked and can not be replied to.