3

I have a module that defines custom attr_accessible attribute as follow:

module MyModule
    include ActiveModel::MassAssignmentSecurity
    %w[var1 var2].each do |key|
        attr_accessible key
    end
end

class MyClass
    include MyModule
end

Since attr_accessible is defined in ActiveModel::MassAssignmentSecurity, I include it in MyModule. However, when I call object = MyClass.first; object.var1

I get the following error:

NoMethodError: undefined method `class_attribute'

Now, this method seems to be defined in active_support/core_ext/class.rb. However, even after

require 'active_support/core_ext/class'

I still get the same error message.

Which module I should include in order to access this method? I am using Rails 4, with protected_attributes gem.

Update1: add include ActiveModel::MassAssignmentSecurity

AdamNYC
  • 19,887
  • 29
  • 98
  • 154
  • 1
    I found here how to include class_attribute from a module: https://www.ruby-forum.com/topic/4406248 (basically it goes inside the "included" block. I don't think it directly answers this question, but will help other people who searched for the general question of including class_attribute in a module. – mahemoff May 09 '14 at 17:11

0 Answers0