I am basically teaching myself how to use RoR and MongoDB at the same time. I am following the very good book / tutorial : http://railstutorial.org/
I decided to replace Sqlite3 by MongoDB using the mongomapper gem. Everything works out about alright, but I am having some non-blocking little issues that I truly wish I could get rid of.
In chapter 6, when working with validation I got 2 issues: - I don't know how to get the validations messages back like with Sqlite3.
The "standard" code is:
$ rails console --sandbox
>> user = User.new(:name => "", :email => "mhartl@example.com")
>> user.save
=> false
>> user.valid?
=> false
>> user.errors.full_messages
=> ["Name can't be blank"]
but if I try to d开发者_StackOverflow中文版o the same with MongoMapper, it throws an error saying that errors is undefined function. So does it mean that this is simply not implemented in mongomapper / mongo driver ? Or is there some other clever way to do this that I could not figure ?
Additional, 2 things here: - I following the exemple in the book to the line, so I was expecting to be able to use the console in sandbox mode, but apparently that does not work either:
(...)ruby-1.9.2-p136@rails3/gems/railties-3.0.3/lib/rails/console/sandbox.rb:1:in `<top (required)>': uninitialized constant ActiveRecord (NameError)
from /Users/Alex/.rvm/gems/ruby-1.9.2-p136@rails3/gems/railties-3.0.3/lib/rails/application.rb:226:in `initialize_console'
from /Users/Alex/.rvm/gems/ruby-1.9.2-p136@rails3/gems/railties-3.0.3/lib/rails/application.rb:153:in `load_console'
from /Users/Alex/.rvm/gems/ruby-1.9.2-p136@rails3/gems/railties-3.0.3/lib/rails/commands/console.rb:26:in `start'
from /Users/Alex/.rvm/gems/ruby-1.9.2-p136@rails3/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
from /Users/Alex/.rvm/gems/ruby-1.9.2-p136@rails3/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Also, in the book they call "user" but I need to call "User" (note the capital U) why is that ? Is it like mangomapper does not follow the Ruby naming convention or something ?
And finally, I am trying to validate the field email with a regex as shown in the tutorial. It does not throws any errors at the code, but whenever I try to insert it just won't ever accept it unless I comment out the :format option...
class User
include MongoMapper::Document
key :name, String, :required => true,
:length => { :maximum => 50 }
key :email, String, :required => true,
# :format => { :with => email_regex },
:uniqueness => { :case_sentitive => false}
timestamps!
end
Any advices you can provide on those topics would help me a lot !
Thanks,
Alex
Add this line to your model:
validates_presence_of :name
.errors
is peculiar to ActiveRecord, so yes you would get an error if you try to use it.
User
will be the class and user
will be an instance of a class - without an example I can't help you here.
To get rid of the problem in the console you need a require 'active_record'
somewhere in your application because some part of your code is still using it but cannot find the definition for it because the Gem has not been required.
Are you using Bundler? Add it to the bundle. Using Ruby Gems, just put that require somewhere appropriate in your application.
Well thanks to stef's reminder of the difference between the class and the instantiated object (Duh !) I realized that I have read this way too fast :)
Since the beginning I was trying to get methods out of my class rather than the instantiated object.
So now, I can fully get the user.errors.full_messages properly...
Which helps with validation :) And hopefully thanks to that I'll be able to get my regex validation sorted !
Alex
Ok I got the validation issue (with the email fixed too).
This code will work as expected:
email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
key :name, String, :length => { :maximum => 50 }
key :email, String
timestamps!
validates_presence_of :name, :email
validates_uniqueness_of :email, :case_sensitive => false
validates_format_of :email, :with =>email_regex
I had a tough time figuring out exactly what would work and what would not... because unless I don't know how to use google anymore, this ain't really documented completely anywhere. You can find some portion of information on slideshares, github, google group etc. relating to mongomapper.
Don't get me wrong, I love mongomapper and the work john has done on it, but now the validation process is a bit messy... part of it still uses the "old" 2.x rails systems and the other part uses the new 3.x rails notation...
I like the new 3.x notation much better, because it allows you centralize all the validation condition around your field all in one place. Like the :length => {:maximum => 50} example above. But to get proper uniqueness and format I had to use the "validates_xxxx" methods outside of the key declaration. This is a bit annoying to mix both styles.
Finally, I wish I could find an official documented description of all possible validation, that would help a lot I believe.
Alex
精彩评论