有没有一种方法可以用DataMapper获取模型的属性?例如:
require 'rubygems'
require 'datamapper'
class User
include DataMapper::Resource
property :id, Serial
property :name, String
end我可以在数组或散列中获取User的属性吗?
发布于 2011-01-13 14:59:15
是的,您可以通过以下方式获取它们
User.properties它将返回一个PropertySet实例,如果需要,您可以将其转换为数组。
发布于 2011-01-13 08:26:31
>> 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
=> Hashhttps://stackoverflow.com/questions/4673961
复制相似问题