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
…
On Jun 7, 11:12 am, “Adinda P.” [email protected] 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
On Sat, Jun 7, 2008 at 6:22 PM, Frederick C.
[email protected]
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
On Jun 8, 8:56 am, “Adinda P.” [email protected] 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).
Fred
On Sun, Jun 8, 2008 at 6:41 PM, Frederick C.
[email protected]
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
On 8 Jun 2008, at 16:25, Adinda P. wrote:
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