【发布时间】:2010-08-26 17:02:19
【问题描述】:
我想通过关联为数组中的每个符号添加一个 has_many 到 activerecord 模型类。例如
PeopleOrganisation::ROLES.each do |role|
has_many role.to_s.pluralize.to_sym, :through => :people_organisations, :source => :person,
:conditions => "people_organisations.role = '#{role.to_s}'" do
def << (object)
PeopleOrganisation.send(:with_scope, :create => {:role => **role**}) { self.concat object }
end
end
end
除了在方法 def 中对角色变量的引用之外,一切正常。这是因为方法 def 不是闭包。有没有办法实现我想要的?
【问题讨论】:
标签: ruby-on-rails ruby activerecord closures