Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Allow classes using validation to specify how attributes should be read

#2935

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
  end
  
  def []=(key, value)
    @data[key] = value
  end

  private
  
  def value_for(key)
    @data[key]
  end
end

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.

Reported by Jamie Hill · July 22nd, 2009 @ 01:13 AM

State: duplicate
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. José Valim
    José Valim
    • State changed from new to duplicate

    August 8th, 2009 @ 11:41 AM