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?
On 4/21/06, zven <zven@nm.ru> 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060420/e0b0166b/attachment.html
On Apr 21, 2006, at 04:23 AM, zven wrote:> 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)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
On Thu, 2006-04-20 at 14:31 -0700, Josh Knowles wrote:> > > On 4/21/06, zven <zven@nm.ru> 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 >Thanks for reply Josh, I still have the same: "undefined method `contents'' for #<Section:0xb74d7bec>" ruby1.8, rails 1.1.0
On Fri, 2006-04-21 at 03:45 -0500, zven wrote:> On Thu, 2006-04-20 at 14:31 -0700, Josh Knowles wrote: > Thanks for reply Josh, I still have the same: "undefined method > `contents'' for #<Section:0xb74d7bec>" > > ruby1.8, rails 1.1.0Problem solved. Thanks.