我对这个define_method做错了什么?

时间:2012-10-02 02:49:38

标签: ruby-on-rails ruby ruby-on-rails-3 metaprogramming

好的,我想为所有类型设置一个named_scope,如下所示

class Variety < ActiveRecord::Base
  TYPES = ["holiday", "party", "other", yet_another]

  Variety::TYPES.each do |role|
    define_method
      scope "#{role.to_sym}_applications", where(:type => role)
    end
  end
end

基本上我希望命名范围以编程方式定义元,以便我可以这样做

Variety.holiday_applications
Variety.party_applications
Variety.other_applications
Variety.yet_another_applications

知道我在使用define方法做错了什么

2 个答案:

答案 0 :(得分:2)

尝试将to_sym移动到整个方法名称。

scope "#{role}_applications".to_sym, where(:type => role)

答案 1 :(得分:0)

 Variety::TYPES.each do |role|
   scope "#{role}_applications".to_sym, where(:type => role)
 end