Mongoid在保存具有特定名称的字段时出错

时间:2012-04-06 15:21:22

标签: ruby-on-rails ruby-on-rails-3 mongodb mongoid

我在rails app中使用mongoid。 rails 3.0.10 ruby​​ 1.9.2p0

当我尝试使用“test”,“link”,“desc”等字段时

Content.new(:test => 'ggg')

Content.new(:link => 'ggg')

Content.new(:desc => 'ggg')

内容是使用“Mongoid :: Document”的模型

它会出现以下错误。 (此错误适用于字段“test”)

我认为mongoid使用一些库,其中包含字段名称以上的内容作为

的函数

可能会产生问题。有没有办法解决这个问题。

ArgumentError: wrong number of arguments (0 for 2..3)
    from /usr/lib/ruby/gems/1.9.1/gems/activemodel-3.0.10/lib/active_model/dirty.rb:155:in `test'
    from /usr/lib/ruby/gems/1.9.1/gems/activemodel-3.0.10/lib/active_model/dirty.rb:155:in `attribute_will_change!'
    from /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.2.4/lib/mongoid/attributes.rb:102:in `block (2 levels) in write_attribute'
    from /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.2.4/lib/mongoid/attributes.rb:100:in `tap'
    from /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.2.4/lib/mongoid/attributes.rb:100:in `block in write_attribute'
    from /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.2.4/lib/mongoid/attributes.rb:170:in `assigning'
    from /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.2.4/lib/mongoid/attributes.rb:98:in `write_attribute'
    from /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.2.4/lib/mongoid/attributes/processing.rb:95:in `process_attribute'
    from /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.2.4/lib/mongoid/attributes/processing.rb:25:in `block in process'
    from /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.2.4/lib/mongoid/attributes/processing.rb:23:in `each_pair'
    from /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.2.4/lib/mongoid/attributes/processing.rb:23:in `process'
    from /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.2.4/lib/mongoid/document.rb:128:in `block in initialize'
    from /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.2.4/lib/mongoid/relations/builders.rb:47:in `building'
    from /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.2.4/lib/mongoid/document.rb:125:in `initialize'
    from (irb):1:in `new'
    from (irb):1
    from /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.10/lib/rails/commands/console.rb:44:in `start'
    from /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.10/lib/rails/commands/console.rb:8:in `start'
    from /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.10/lib/rails/commands.rb:23:in `<top (required)>'
    from /home/harshal/simple-cms/branches/1.0/script/rails:6:in `require'
    from /home/harshal/simple-cms/branches/1.0/script/rails:6:in `<top (required)>'
    from -e:1:in `load'
    from -e:1:in `<main>'

1 个答案:

答案 0 :(得分:1)

使用最新的一切,ruby(1.9.3),rails(3.2.3),mongoid(2.4.7)我尝试了以下内容:

没有设置字段的模型,全部是动态的:

class Content
  include Mongoid::Document
end

Rails控制台:

1.9.3p125 :011 > c = Content.new(:test => "test", :link=> "link", :desc => "desc")
 => #<Content _id: 4f7f49f5add3617fae000003, _type: nil, test: "test", link: "link", desc: "desc"> 
1.9.3p125 :012 > c.save
 => true 
1.9.3p125 :013 > Content.first
 => #<Content _id: 4f7f49f5add3617fae000003, _type: nil, test: "test", link: "link", desc: "desc"> 
1.9.3p125 :014 >

静态设置所有字段的模型:

class Content
  include Mongoid::Document
  field :test, :type => String
  field :link, :type => String
  field :desc, :type => String
end

再次使用Rails控制台:

Loading development environment (Rails 3.2.3)
1.9.3p125 :001 > c = Content.new
 => #<Content _id: 4f7f4652add3617ec4000001, _type: nil, test: nil, link: nil, desc: nil> 
1.9.3p125 :002 > c.test = 'tyler'
 => "tyler" 
1.9.3p125 :003 > c.save
 => true 
1.9.3p125 :004 > Content.first
 => #<Content _id: 4f7f4652add3617ec4000001, _type: nil, test: "tyler", link: nil, desc: nil>