module ActiveRecord::AttributeMethods::BeforeTypeCast - activerecord-4.2.3 Documentation

# module ActiveRecord::AttributeMethods::BeforeTypeCast

# Active Record Attribute Methods Before Type Cast¶ ↑

`ActiveRecord::AttributeMethods::BeforeTypeCast` provides a way to read the value of the attributes before typecasting and deserialization.

    class Task < ActiveRecord::Base
    end

    task = Task.new(id: '1', completed_on: '2012-10-21')
    task.id           # => 1
    task.completed_on # => Sun, 21 Oct 2012

    task.attributes_before_type_cast
    # => {"id"=>"1", "completed_on"=>"2012-10-21", ... }
    task.read_attribute_before_type_cast('id')           # => "1"
    task.read_attribute_before_type_cast('completed_on') # => "2012-10-21"

In addition to read_attribute_before_type_cast and attributes_before_type_cast, it declares a method for all attributes with the `*_before_type_cast` suffix.

    task.id_before_type_cast           # => "1"
    task.completed_on_before_type_cast # => "2012-10-21"

### Public Instance Methods

attributes_before_type_cast() click to toggle source

Returns a hash of attributes before typecasting and deserialization.

    class Task < ActiveRecord::Base
    end

    task = Task.new(title: nil, is_done: true, completed_on: '2012-10-21')
    task.attributes
    # => {"id"=>nil, "title"=>nil, "is_done"=>true, "completed_on"=>Sun, 21 Oct 2012, "created_at"=>nil, "updated_at"=>nil}
    task.attributes_before_type_cast
    # => {"id"=>nil, "title"=>nil, "is_done"=>true, "completed_on"=>"2012-10-21", "created_at"=>nil, "updated_at"=>nil}

    # File lib/active_record/attribute_methods/before_type_cast.rb, line 60
    def attributes_before_type_cast
      @attributes.values_before_type_cast
    end

read_attribute_before_type_cast(attr_name) click to toggle source

Returns the value of the attribute identified by `attr_name` before typecasting and deserialization.

    class Task < ActiveRecord::Base
    end

    task = Task.new(id: '1', completed_on: '2012-10-21')
    task.read_attribute('id')                            # => 1
    task.read_attribute_before_type_cast('id')           # => '1'
    task.read_attribute('completed_on')                  # => Sun, 21 Oct 2012
    task.read_attribute_before_type_cast('completed_on') # => "2012-10-21"
    task.read_attribute_before_type_cast(:completed_on)  # => "2012-10-21"

    # File lib/active_record/attribute_methods/before_type_cast.rb, line 46
    def read_attribute_before_type_cast(attr_name)
      @attributes[attr_name.to_s].value_before_type_cast
    end

### Private Instance Methods

attribute_before_type_cast(attribute_name) click to toggle source

Handle *_before_type_cast for method_missing.

    # File lib/active_record/attribute_methods/before_type_cast.rb, line 67
    def attribute_before_type_cast(attribute_name)
      read_attribute_before_type_cast(attribute_name)
    end

attribute_came_from_user?(attribute_name) click to toggle source

    # File lib/active_record/attribute_methods/before_type_cast.rb, line 71
    def attribute_came_from_user?(attribute_name)
      @attributes[attribute_name].came_from_user?
    end