method

class_inheritable_accessor

rails latest stable - Class: Class

Method deprecated or moved

This method is deprecated or moved on the latest stable version. The last existing version (v3.1.0) is shown here.

class_inheritable_accessor(*syms)
public

No documentation available.

# File activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb, line 113
  def class_inheritable_accessor(*syms)
    class_inheritable_reader(*syms)
    class_inheritable_writer(*syms)
  end

1Note

Gotcha with class_inheritable_accessor and cloneing the attribute values

jesseclark ยท Aug 18, 2009

The key thing to note from post is that class_inheritable_accessor copies the value from the parent class at inherit time. So, if you are setting a default value of an array and doing something like the following you might end up with unintended results:

>> A.class_inheritable_foo << 'from a'
=> ["from a"]
>> B.class_inheritable_foo << 'from b'
=> ["from a", "from b"]
>> C.class_inheritable_foo << 'from c'
=> ["from a", "from b", "from c"]

However, if you use the form:

class << self
  def class_instance_foo
    @class_instance_processors ||= []
  end
end

The original values aren't copied from the parent class when you reference the class for the first time:

>> A.class_instance_foo << 'from a'
=> ["from a"]
>> B.class_instance_foo << 'from b'
=> ["from b"]
>> C.class_instance_foo << 'from c'
=> ["from c"]