2

我在 ruby​​ on rails 上使用 mongoid 的保存选项,它给我一个错误

  can't convert Fixnum into String 

我的模型文件是

产品.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  

状态.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

我可以查看puts中的值,但它没有通过在
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'

提前致谢

4

2 回答 2

1

我不确定您为什么会收到该错误,但更好的方法可能是使用关系而不是手动将 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
于 2013-09-18T12:58:56.870 回答
0

我找到了解决方案,但不确定它的正确方法,在我删除的产品模型上

 include Mongoid::Versioning

这样我可以通过命令保存将它保存在产品模型上。

于 2013-09-19T11:23:02.363 回答