Adinda Praditya
2008-Jun-07 10:12 UTC
Can create record but can not update it => has_many through
Hi All, I have a problem using has_many through association. I can create a product without problem, but when i edit it, i got this NoMethodError: undefined method `reciters='' for #<Product:0xb71a77e8> Did i miss something to code? I provided the codes below. Please help Thanks, Dida product_controller.rb =====================def create @product = Product.new(params[:product]) if @product.save unless params[:reciters].blank? @product.reciters << Qori.find(params[:reciters]) end flash[:notice] = ''New product successfully created.'' redirect_to :action => ''index'' else render :action => ''new'' end end def update @product = Product.find(params[:id]) @product.update_attributes(params[:product]) unless params[:reciters].blank? @product.reciters = Qori.find(params[:reciters]) end if @product.save flash[:notice] = ''Product has been updated.'' redirect_to :action => ''show'', :id => @product else render :action => ''edit'' end end ================== product_controller.rb product.rb =================class Product < ActiveRecord::Base ... has_many :pilihan_qoris has_many :reciters, :through => :pilihan_qoris, :source => :qori ... qori.rb ================class Qori < ActiveRecord::Base has_many :products has_many :pilihan_qoris has_many :creations, :through => :pilihan_qoris, :source => :product ... ================ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jun-07 10:22 UTC
Re: Can create record but can not update it => has_many through
On Jun 7, 11:12 am, "Adinda Praditya" <apradi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi All, > > I have a problem using has_many through association. I can create a product > without problem, but when i edit it, i got this NoMethodError: > > undefined method `reciters='' for #<Product:0xb71a77e8> > > Did i miss something to code? I provided the codes below. Please help >has_many :through doesn''t have foos= before rails 2.1 Fred> Thanks, > > Dida > > product_controller.rb > =====================> def create > @product = Product.new(params[:product]) > > if @product.save > unless params[:reciters].blank? > @product.reciters << Qori.find(params[:reciters]) > end > flash[:notice] = ''New product successfully created.'' > redirect_to :action => ''index'' > else > render :action => ''new'' > end > end > > def update > @product = Product.find(params[:id]) > > @product.update_attributes(params[:product]) > > unless params[:reciters].blank? > @product.reciters = Qori.find(params[:reciters]) > end > > if @product.save > flash[:notice] = ''Product has been updated.'' > redirect_to :action => ''show'', :id => @product > else > render :action => ''edit'' > end > end > > ================== product_controller.rb > > product.rb > =================> class Product < ActiveRecord::Base > ... > has_many :pilihan_qoris > has_many :reciters, :through => :pilihan_qoris, :source => :qori > ... > > qori.rb > ================> class Qori < ActiveRecord::Base > has_many :products > has_many :pilihan_qoris > has_many :creations, :through => :pilihan_qoris, :source => :product > ... > ================--~--~---------~--~----~------------~-------~--~----~You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Adinda Praditya
2008-Jun-08 07:56 UTC
Re: Can create record but can not update it => has_many through
On Sat, Jun 7, 2008 at 6:22 PM, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > has_many :through doesn''t have foos= before rails 2.1 >Would you explain your answer a bit more? How come i can create a record without the error as i got when i was trying to update it? Any reference? Other solution to use instead? Thanks, Adinda P --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jun-08 10:41 UTC
Re: Can create record but can not update it => has_many through
On Jun 8, 8:56 am, "Adinda Praditya" <apradi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sat, Jun 7, 2008 at 6:22 PM, Frederick Cheung <frederick.che...@gmail.com> > wrote: > > > > > has_many :through doesn''t have foos= before rails 2.1 > > Would you explain your answer a bit more? How come i can create a record > without the error as i got when i was trying to update it? Any reference? > Other solution to use instead? >Pretty much what I said. if you have has_many :customers you can do customers= ..., but if you have has_many :customers, :through => :something_else, then until 2.1 you can''t do it. It''s just not implemented (you''ll have to do it yourself using the methods that are available - the documentation on associations has a table of which association types have which methods). Fred> Thanks, > > Adinda P--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Adinda Praditya
2008-Jun-08 15:25 UTC
Re: Can create record but can not update it => has_many through
On Sun, Jun 8, 2008 at 6:41 PM, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Pretty much what I said. if you have has_many :customers you can do > customers= ..., but if you have has_many :customers, :through > => :something_else, then until 2.1 you can''t do it. It''s just not > implemented (you''ll have to do it yourself using the methods that are > available - the documentation on associations has a table of which > association types have which methods). >I tried with @product.reciter_ids = params[:reciters] before and i also got NoMethodError. Please help. unless params[:reciters].blank? @product.reciters = Qori.find(params[:reciters]) end Thanks, Adinda P Fred> >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jun-08 16:19 UTC
Re: Can create record but can not update it => has_many through
On 8 Jun 2008, at 16:25, Adinda Praditya wrote:> On Sun, Jun 8, 2008 at 6:41 PM, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > > wrote: > > Pretty much what I said. if you have has_many :customers you can do > customers= ..., but if you have has_many :customers, :through > => :something_else, then until 2.1 you can''t do it. It''s just not > implemented (you''ll have to do it yourself using the methods that are > available - the documentation on associations has a table of which > association types have which methods). > > I tried with @product.reciter_ids = params[:reciters] before and i > also got NoMethodError. Please help. >foo_ids= doesn''t exist either. You need to use <<, delete etc... (or upgrade to 2.1) Fred> unless params[:reciters].blank? > @product.reciters = Qori.find(params[:reciters]) > end > > Thanks, > > Adinda P > > > Fred > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---