質問

I am working on a Padrino project that uses Datamapper as the ORM. I have a User class that looks something like this:

class User
  include DataMapper::Resource

  property :id, Serial                                           
  property :username, String,
  property :password, String
  property :confirmed, Boolean
  ...<snip>...
end

Now, in my users controller, I have this snippet of code:

user = User.get(current_user)
var1 = user.username
var2 = user.confirmed

Now, var1 is populated perfectly fine with the contents of the username property of the model. However, the next line throws the following error:

NoMethodError at /users/blah
undefined method 'confirmed' for User:Class

Looks like ruby is trying to treat the .confirmed as a method and not a property? It seems to be happening on Boolean properties, and not String or Integer properties.

Is there something I have missed out?

役に立ちましたか?

解決

DataMapper properties are simply Ruby methods that DM makes for you (they are made using Ruby's meta-programming features when the property method is called). You could try

 p user.methods.sort - Object.methods 

to get a listing of properties on the User object you created.

DM will have chosen to make the method user.confirmed? for the :confirmed property - it follows a loose Ruby convention of naming boolean methods with a ? on the end. I have checked this by exploring the DataMapper code here: https://github.com/datamapper/dm-core/blob/master/lib/dm-core/property.rb

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top