0

好的,所以我想为所有类型设置一个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

知道我在定义方法上做错了什么

4

2 回答 2

2

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

scope "#{role}_applications".to_sym, where(:type => role)
于 2012-10-02T02:53:47.733 回答
0
 Variety::TYPES.each do |role|
   scope "#{role}_applications".to_sym, where(:type => role)
 end
于 2012-10-02T02:56:13.797 回答