Reputation: 9373
I am getting started with a non-SQL database (specifically, MongoDB) and Ruby on Rails because I believe flexible schemas will be an advantage. Right now, I'm confused because some things I expect to "just work" aren't working. Specifically, I have a "method missing" error on one of my pages.
NoMethodError in Users#new
undefined method `email' for #<User _id: BSON::ObjectId('4eb8cbcaef704c02da000017')>
Extracted source (around line #13):
10: <tbody><tr>
11: <td><%= f.label :email %>:</td>
12: <td>
13: <%= f.text_field :email, :placeholder => "your email address" %>
14: </td>
15: </tr>
16: <tr>
In the console:
>> User.new.email
NoMethodError: undefined method `email' for #<User _id: BSON::ObjectId('4eb8cc4def704c0358000005')>
from /Library/Ruby/Gems/1.8/gems/activemodel-3.1.1/lib/active_model/attribute_methods.rb:385:in `method_missing'
from (irb):5
The model looks like: (note class User does not inherit from any Active Record super class... does this matter?)
class User
include MongoMapper::Document
attr_accessor :password
attr_accessible :username, :email, :password
email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :username, :presence => true,
:format => { :with => /^[a-zA-Z][a-zA-Z0-9_]+$/ },
:length => { :maximum => 32, :minimum => 4 }
validates :email, :presence => true,
:format => { :with => email_regex },
:uniqueness => { :case_sensitive => false }
validates :password, :presence => true,
:length => { :within => 5..32 },
:confirmation => true
(etc...)
Upvotes: 1
Views: 1523
Reputation: 11
your problem is that with the comand "User.new.email" you are trying to use a method called "email" but i think that email is an attribute not a method
Upvotes: 1
Reputation: 434665
If you look at the error messages you'll see things like this:
#<User _id: BSON::ObjectId('4eb8cc4def704c0358000005')>
so your MongoMapper backed models only have the default _id
property. You'll need to tell MongoMapper what the other properties are:
class User
include MongoMapper::Document
key :password, String
key :username, String
key :email, String
#...
The MongoMapper documentation can be difficult to navigate but the important bits are there.
So MongoMapper does use schemas of a sort.
Upvotes: 3