我有这样的代码:在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我加了 –