Browse the Ruby on Rails Community.

You are here: Browse Railsplugins Attribute Mapper

Attribute Mapper

== AttributeMapper

The AttributeMapper plugin provides a transparent interface for mapping symbolic representations to a column in the database of a more primitive type. For example, rather than hardcoding values like 1 or 2 to represent that a Ticket model’s status column is “open” or “closed” you would create the following mapping:

class Ticket < ActiveRecord::Base
  map_attribute :status, :to => {:open => 1, :closed => 2}
end

You can now get and set the status column symbolically:

ticket.status = :open
ticket.status
=> :open

Internally, the integer 1 will be stored in the database.

An authority list of the mapping is available as a class method which is the pluralized version of the attribute:

Ticket.statuses
=> {:open => 1, :closed => 2}

The primitive values of the mapping can always be used to assign the column, though the getter for the attribute will always return the higher level symbolic representation.

ticket.status = 1
ticket.status
=> :open

NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly

Users


See all details


Membership

+ Join this railsplugin

Record Maintainer

'None'