0

我在 Rails 应用程序中使用 mongoid。轨道 3.0.10 红宝石 1.9.2p0

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

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

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

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

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

它给出了以下错误。(此错误适用于“测试”字段)

我认为 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>'
4

1 回答 1

1

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

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

class Content
  include Mongoid::Document
end

导轨控制台:

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>
于 2012-04-06T19:50:25.813 回答