好了,基本上你想模仿无模式数据库,因为您希望不同的记录具有不同的属性。只要你只有一个自定义属性pr。记录,这可能是工作,但如果你的记录有差异比一般多个属性,那么你可能要考虑有多种型号,看看到hstore数据类型或寻找到一个文件数据库,如MongoDB的。
更新
重读你的问题,我想我有一个更好的解决办法,所以我删除了原来的一个。
我会打电话给你ThingAttributes类什么,我认为这是 - 一个CustomAttribute类。因为每个记录代表一个自定义属性。事物可以有许多(在你的例子中是五个)自定义属性。
所以,你可以这样做:
class CustomAttribute < ActiveRecord::Base
belongs_to :thing
attr_accessible :name, :value
end
class Thing < ActiveRecord::Base
has_many :custom_attributes
end
现在你可以找到一个东西,写
my_thing = Thing.find(3)
然后你可以找到它的custom_attributes,通过写
my_thing.custom_attributes
这将返回一组自定义属性。然而,你是(出于某种原因)要求散列。这也可以做到。在您的Thing类中,定义此方法:
def custom_attributes_hash
custom_hash = {}
self.custom_attributes.each do |attr|
custom_hash[attr.name] = attr.value
end
custom_hash
end
现在,您可能希望能够以便捷的方式设置属性。在你的课堂上定义这个。
def set_custom_attribute(name, value)
return unless name.present? # Short circuits method if an empty string or nil is being used as name.
custom_attribute = self.custom_attributes.find_by_name(name) # Attemps to find custom attribute with the name
if custom_attribute.nil? # Executes block if no attribute was found
return unless value.present? # Short circuits method if nil or empty string was passed as value
self.custom_attributes.create(name: name, value: value) # Creates and saves new custom attribute
else
if value.present? # Updates existing attribute if passed is not an empty string or nil.
custom_attribute.update_attribute(:value, value)
else
custom_attribute.destroy # Deletes custom attribute from DB if set_custom_attribute was called with a nil or empty value.
end
end
end
你能澄清你的问题。我对你的措辞感到困惑。 – jason328 2013-05-10 19:50:31
对不起。我认为我的头衔可能是错误的。我会尽力澄清。 – netricate 2013-05-10 19:58:18
你想要这个散列究竟是什么?每个记录标签的汇总?该表应该是一个统一的散列图吗?我的意思是,如果你有50条记录,你想要一个有50个键和50个值的散列吗?你会接受多个具有相同标签(密钥)的记录吗? – 2013-05-10 20:20:07