【发布时间】:2012-11-09 02:58:35
【问题描述】:
我想知道是否可以在我的类中添加与此类似的内容,并让它构建一个我可以在其他类中引用的类属性。我不想记住 id,也不想在权重表中的 id 发生变化时不断更新 id。我也不想将权重表锁定为一组特定的 id。
所以我想做如下的事情:
class Weight < ActiveRecord::Base
attr_accessible :name
@@kind = {
Weight.all.each do |weight|
weight.name.delete(' ').underscore.to_sym: weight.id,
end
}
cattr_reader :kind, :instance_accessor => false
end
然后在代码的其他区域我可以做类似的事情..
scope :light, where(weight_id, Weight::kind(:light))
我确信有一些神奇的 ruby 方法,我只是不确定正确的语法。
【问题讨论】:
标签: ruby ruby-on-rails-3 metaprogramming