我有這樣的代碼:在Rails的更新方法更新有關的模型數據
def update
@oil = Oil.find(params[:id])
@product_types = ProductType.all
if @oil.update_attributes(params[:oil])
if @oil.other_products_cross_lists.update_attributes(:cross_value => @oil.model.to_s.gsub(/\s+/, "").upcase)
redirect_to admin_oils_path
end
else
render :layout => 'admin'
end
end
但是當我運行它,我得到:
undefined method `update_attributes' for #<ActiveRecord::Relation:0x007f7fb4cdc220>
,並沒有更新我的other_products_cross_lists ......此外,我嘗試update_attribute並得到相同的錯誤。
我做錯了什麼?
而且當我運行我的破壞方法
def destroy
@oil = Oil.find(params[:id])
if @oil.destroy
if @oil.other_products_cross_lists.destroy
redirect_to admin_oils_path
end
else
render :layout => 'admin'
end
end
other_products_cross_lists並沒有摧毀...
我怎樣才能解決這個問題?
型號:
class Oil < ActiveRecord::Base
has_many :other_products_cross_lists, :foreign_key => 'main_id'
class OtherProductsCrossList < ActiveRecord::Base
belongs_to :oil
請問您可以發佈'Oil'的模型定義。 –
@MartinM我加了 –