[英]Get DataMapper Model Properties
有没有办法使用DataMapper获取模型的属性? 例如:
require 'rubygems'
require 'datamapper'
class User
include DataMapper::Resource
property :id, Serial
property :name, String
end
我可以在数组或哈希中获取User
的属性吗?
是的,你可以得到它们
User.properties
它将返回一个PropertySet实例,如果需要,可以将其转换为数组。
>> 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.