Ruby on Rails v3.2.2 を使用しています。モジュールで、クラスを「動的に」開こうとして、ローカル変数を使用する Ruby on Rails の「スコープ メソッド」を次のように追加しようとしています。
module MyModule
extend ActiveSupport::Concern
included do
# Note: The `CLASS_NAME` is not the class where `MyModule` is included. That
# is, for instance, if the including class of `MyModule` is `Article` then
# the `CLASS_NAME` is `User`.
CLASS_NAME = self.get_class_name.constantize # => User
counter_cache_column = self.get_counter_cache # => "counter_count"
class CLASS_NAME
def self.order_by_counter
order("#{counter_cache_column} DESC")
end
end
end
end
上記のコードを実行すると、次のエラーが発生します。
NameError
undefined local variable or method `counter_cache_column' for #<Class:0x0000010775c558>
counter_cache_column
モジュールのコンテキストで in が呼び出されないために発生します。order_by_counter
スコープメソッドを適切に記述するにはどうすればよいですか?
おまけ:上記の「非常に動的な」実装についてアドバイスはありますか?