Note that there are some explanatory texts on larger screens.

plurals
  1. POundefined method 'delete' for String:Class when using Mongoid and creating records via console
    text
    copied!<p>I have a class that looks like this: </p> <pre><code>class SearchService include Mongoid::Document key :name, String field :url, String field :searchBaseUrl, String validates_presence_of :name, :url, :searchBaseUrl validates_uniqueness_of :name end </code></pre> <p>The first issue here is that I was using <code>validates_presence_of</code> incorrectly or so it would seem. I commented the line out and I could create the class instance at the console with no problem. But when I tried to create the object with validates_presence_of in place I got an error: </p> <pre><code>NoMethodError: undefined method `delete' for String:Class from D:/Ruby193/lib/ruby/gems/1.9.1/gems/mongoid-2.3.3/lib/mongoid/fields.rb:230:in `add_field' from D:/Ruby193/lib/ruby/gems/1.9.1/gems/mongoid-2.3.3/lib/mongoid/fields.rb:145:in `field' </code></pre> <p>Is this error message meaningful? I'm having a hard time seeing the relationship between a missing method on the String class and me using <code>validates_presence_of</code> incorrectly. If I could pick only one thing about Ruby that bugs me it would have to be terrible error messages. </p> <p>The <a href="http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html" rel="nofollow">rails docs</a> say that validates_presence_of is for association. But the <a href="http://www.mongodb.org/display/DOCS/MongoDB+Data+Modeling+and+Rails" rel="nofollow">mongo db page</a> says that it's used for required fields. Are the mongodb docs incorrect? </p> <p>I'm trying to decide if this is an issue I should report to the mongoid team or if I'm just not understanding how the language works. </p> <p><strong>Update</strong>: OK now I'm getting a very similar error in a class that doesn't use validates_presence_of at all. So it's some other issue (even though commenting out that line fixed it initially). </p> <p><strong>Update2</strong>: This appears to me to be bug in mongoid. Changing this line: </p> <p><code>field :name</code> to <code>field :name, String</code></p> <p>will cause the error to show up. Seems that any field that is defined with a type will mess up mongoid. Or those aren't supposed to be there anymore? The mongodb docs (linked above) have code that looks like <code>key :votes, Integer, :default =&gt; 0</code> so if it isn't valid the docs are wrong. </p> <p>Rails 3.1.1, Ruby 1.9.3</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload