开发者

Get DataMapper Model Properties

Is there a way to get the properties of a model with DataMapper? For example:

require 'rubygems'
require 'datamapper'

class User
  include DataMapper::Resource

  p开发者_运维技巧roperty :id, Serial
  property :name, String
end

Could I get the properties of User in an array or a hash?


Yes, you can get them with

User.properties

it will return an instance of PropertySet which you can cast into an Array if you want.


>> u = User.new
=> #<User @id=nil @name=nil>
>> u.id = 1
=> 1
>> u.name = "hello"
=> "hello"
>> u.attributes
=> {:name=>"hello", :id=>1}
>> u.attributes.class
=> Hash
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜