Virtual attributes in Rails

There are certain senarios when dealing with data in rails, when a certain data is required to be taken from user, but not has be stored directly as it in database. Or in other times it could be, there is no column in database for that field, but still that particular field is required to be placed in form.

Suppose there is no field email_confirmation which it is required to be placed in the form and that field is not added in database for user table in such case attr_accessor. This attr_accessor provides getter and setter methods for that particular attribute. And to get that values for mass assignment attr_accessible is used such as in new(attrs) or up update_attributes(attrs).

class User < ActiveRecord::Base
  attr_accessible :email_confirmation
  attr_accessor :email_confirmation
About these ads