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