Rails 4 - public_send (ключ, значение) вызывает исключение NoMethodError: неопределенный метод `

У меня есть класс Role, определенный как:

class Role < ActiveRecord::Base
  include Filterable
  has_and_belongs_to_many :users
end

Проблема с фильтруемой моделью:

        module Filterable
          extend ActiveSupport::Concern

          module ClassMethods
            def filter(filtering_params)
              results = self.where(nil) # create an anonymous scope
              filtering_params.each do |key, value|
                results = results.public_send(key, value) if value.present?
              end
              results
            end
          end
        end

Результаты = строка self.where(nil), возвращает все роли

  Role Load (1.1ms)  SELECT "roles".* FROM "roles"
  #<Role:0x007fd22d235080>
  #<Role:0x007fd22d234ea0>
  #<Role:0x007fd22d234cc0>

но строка results.public_send(key, value) вызывает ошибку:

results.public_send(key, value)
*** NoMethodError Exception: undefined method `company' for #       <Role::ActiveRecord_Relation:0x007fd232499da8>

Если я проверю:

byebug) results.first
#<Role:0x007fd22d235080> 

(byebug) results.first.inspect
#<Role id: 1, company: "internal", group: "accounting", function: 
"employee", activities: {}, created_at: "2015-09-16 06:54:19",
updated_at: "2015-09-16 06:54:19">

(byebug) results.first.company
# nothing returned !
(byebug) results.first[:company]
internal

The Role class doesn't need any attribute accessor as it's an ActiveRecord class...  the company, group, function should ahem getter/setter defined .. where am I wrong ?

спасибо за отзыв


person Community    schedule 16.09.2015    source источник
comment
Чего вы пытаетесь достичь с помощью этого кода?   -  person Alexey Shein    schedule 16.09.2015
comment
Цель: избавиться от области действия и метода класса для запроса и фильтрации моделей.. и теперь он отлично работает, кстати, я не знаю, как задать повторяющийся вопрос, который я вижу сейчас вверху, иду в пост... вот почему я ответил внизу .. извините, что не сделал это хорошо ...   -  person    schedule 16.09.2015