define_model_callbacks

define_model_callbacks(*callbacks) Instance Public methods #define_model_callbacks accepts the same options define_callbacks does, in case you want to overwrite a default. Besides that, it also accepts an :only option, where you can choose if you want all types (before, around or after) or just some. define_model_callbacks :initializer, only: :after Note, the only: <type> hash will apply to all callbacks defined on that method call. To get around this you can call the #defin

respond_to_without_attributes?

respond_to_without_attributes?(method, include_private_methods = false) Instance Public methods A Person instance with a name attribute can ask person.respond_to?(:name), person.respond_to?(:name=), and person.respond_to?(:name?) which will all return true. respond_to?

respond_to?

respond_to?(method, include_private_methods = false) Instance Public methods Also aliased as: respond_to_without_attributes?

method_missing

method_missing(method, *args, &block) Instance Public methods Allows access to the object attributes, which are held in the hash returned by attributes, as though they were first-class methods. So a Person class with a name attribute can for example use Person#name and Person#name= and never directly use the attributes hash â except for multiple assigns with ActiveRecord::Base#attributes=. It's also possible to instantiate related objects, so a Client class belonging to the cli

attribute_missing

attribute_missing(match, *args, &block) Instance Public methods attribute_missing is like method_missing, but for attributes. When method_missing is called we check to see if there is a matching attribute method. If so, we tell attribute_missing to dispatch the attribute. This method can be overloaded to customize the behavior.

undefine_attribute_methods

undefine_attribute_methods() Instance Public methods Removes all the previously dynamically defined methods from the class. class Person include ActiveModel::AttributeMethods attr_accessor :name attribute_method_suffix '_short?' define_attribute_method :name private def attribute_short?(attr) send(attr).length < 5 end end person = Person.new person.name = 'Bob' person.name_short? # => true Person.undefine_attribute_methods person.name_short? # => No

define_attribute_methods

define_attribute_methods(*attr_names) Instance Public methods Declares the attributes that should be prefixed and suffixed by ActiveModel::AttributeMethods. To use, pass attribute names (as strings or symbols), be sure to declare define_attribute_methods after you define any prefix, suffix or affix methods, or they will not hook in. class Person include ActiveModel::AttributeMethods attr_accessor :name, :age, :address attribute_method_prefix 'clear_' # Call to define_attr

define_attribute_method

define_attribute_method(attr_name) Instance Public methods Declares an attribute that should be prefixed and suffixed by ActiveModel::AttributeMethods. To use, pass an attribute name (as string or symbol), be sure to declare define_attribute_method after you define any prefix, suffix or affix method, or they will not hook in. class Person include ActiveModel::AttributeMethods attr_accessor :name attribute_method_suffix '_short?' # Call to define_attribute_method must appe

attribute_method_suffix

attribute_method_suffix(*suffixes) Instance Public methods Declares a method available for all attributes with the given suffix. Uses method_missing and respond_to? to rewrite the method. #{attr}#{suffix}(*args, &block) to attribute#{suffix}(#{attr}, *args, &block) An attribute#{suffix} instance method must exist and accept at least the attr argument. class Person include ActiveModel::AttributeMethods attr_accessor :name attribute_method_suffix '_short?' define_a

attribute_method_prefix

attribute_method_prefix(*prefixes) Instance Public methods Declares a method available for all attributes with the given prefix. Uses method_missing and respond_to? to rewrite the method. #{prefix}#{attr}(*args, &block) to #{prefix}attribute(#{attr}, *args, &block) An instance method #{prefix}attribute must exist and accept at least the attr argument. class Person include ActiveModel::AttributeMethods attr_accessor :name attribute_method_prefix 'clear_' define_at