This project is archived and is in readonly mode.

#2935 ✓duplicate
Jamie Hill

Allow classes using validation to specify how attributes should be read

Reported by Jamie Hill | July 22nd, 2009 @ 01:13 AM | in 2.x

This patch adds a hook method to validations allowing a class to define how it's attributes should be acquired. This is useful should the class not use the default of having a method for each attribute and instead store values in a hash e.g.

class MyClass
  include ActiveModel::Validations

  def initialize(data = {})
    @data = data
  def []=(key, value)
    @data[key] = value

  def value_for(key)

By overriding the value_for method you have control over how attribute values are retrieved.

As Rails 3 is all about not having to hack the framework and instead provide hooks I believe this is a great addition an have use-cases right away.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket