我在轨道上的红宝石上使用mongoid上的保存选项,它给我一个错误
can't convert Fixnum into String
我的模型文件是
product.rb
class Product
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Versioning
field :product_role, :type => String
field :status_id, type: Moped::BSON::ObjectId
field :status_name,type: String
end
status.rb
class Status
include Mongoid::Document
field :name, type: String
index :name => 1
end
控制器代码是
product_id = "523741002a6c6e8d1a000002",
status_name = "Pending Review"
product_detail_status = Product.where(id:product_id).first
status = Status.where(name:status_name).first
puts "##############"
puts product_detail_status.id.to_s
puts status.name.to_s
puts status.id.to_s
puts "##############"
product_detail_status.status_name = status.name.to_s
product_detail_status.status_id = status.id
product_detail_status.save
我可以查看看跌期权中的值,但不能通过其抛出错误的方式进行保存 product_detail_status.save为“无法将Fixnum转换为字符串”
Completed 500 Internal Server Error in 657ms
TypeError - can't convert Fixnum into String:
mongoid (3.1.4) lib/mongoid/versioning.rb:53:in `revise'
activesupport (3.2.13) lib/active_support/callbacks.rb:407:in `
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
mongoid (3.1.4) lib/mongoid/callbacks.rb:130:in `run_callbacks'
mongoid (3.1.4) lib/mongoid/persistence/modification.rb:24:in `prepare'
mongoid (3.1.4) lib/mongoid/persistence/operations/update.rb:45:in `persist'
mongoid (3.1.4) lib/mongoid/persistence.rb:145:in `update'
mongoid (3.1.4) lib/mongoid/persistence.rb:82:in `save'
提前致谢
答案 0 :(得分:1)
我不确定您为什么会收到该错误,但更好的方法可能是使用relations而不是手动将status_id和status_name添加到产品型号
class Product
include Mongoid::Document
field :product_role, :type => String
has_one :status
end
然后在您的控制器中,分配产品的状态
product_detail_status = Product.where(id:product_id).first
status = Status.where(name:status_name).first
product_detail_status.status = status
答案 1 :(得分:0)
我找到了解决方案,但我不确定它的正确方法,我删除的产品型号
include Mongoid::Versioning
我可以通过命令保存将其保存在产品型号上。