zven
April 20, 2006, 11:28pm
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?
zven
April 20, 2006, 11:34pm
2
On 4/21/06, zven [email protected] 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
zven
April 20, 2006, 11:43pm
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
zven
April 20, 2006, 11:46pm
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
zven
April 20, 2006, 11:52pm
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.