Ruby on rails 创建/更新对象时,如何使Mongoid忽略模型中未设置的参数属性?
我试图通过一个JSON API为我的MongoDB数据库种子,只想为每个对象保存特定的属性 运行种子文件时,出现以下错误:Ruby on rails 创建/更新对象时,如何使Mongoid忽略模型中未设置的参数属性?,ruby-on-rails,mongodb,mongoid,Ruby On Rails,Mongodb,Mongoid,我试图通过一个JSON API为我的MongoDB数据库种子,只想为每个对象保存特定的属性 运行种子文件时,出现以下错误: Mongoid::Errors::UnknownAttribute: Problem: Attempted to set a value for 'block' which is not allowed on the model FoodTruck. Summary: Without including Mongoid::Attributes::Dynamic i
Mongoid::Errors::UnknownAttribute:
Problem:
Attempted to set a value for 'block' which is not allowed on the model FoodTruck.
Summary:
Without including Mongoid::Attributes::Dynamic in your model and the attribute does not already exist in the attributes hash, attempting to call FoodTruck#block= for it is not allowed. This is also triggered by passing the attribute to any method that accepts an attributes hash, and is raised instead of getting a NoMethodError.
Resolution:
You can include Mongoid::Attributes::Dynamic if you expect to be writing values for undefined fields often.
我不想包括Mongoid::Attributes::Dynamic,因为我不想保存那些没有添加到模型中的特定属性
我尝试了这里找到的两个答案(),但这两个答案都不适用于我
我如何告诉mongoid忽略参数中的任何哈希键,这些哈希键在我尝试创建或更新属性时没有添加到模型中?尝试以下操作:
FoodTruck.create(params.as_json(only: FoodTruck.fields.keys))
foodtruck.update_attributes(params.as_json(only: FoodTruck.fields.keys))
试试这个:
FoodTruck.create(params.as_json(only: FoodTruck.fields.keys))
foodtruck.update_attributes(params.as_json(only: FoodTruck.fields.keys))
迟到了,但我认为有人可以从中受益
@object.update_attributes(params.except(:block))
迟到了,但我认为有人可以从中受益
@object.update_attributes(params.except(:block))