activerecord readonly attributes

I hope attr_accessor can work for this purpose. It wont be inserted in db.

K.Arunmohan probably means attr_accessible and not attr_accessor. attr_accessor is a ruby function that generates instance variables and getters/setters for these.

If you use attr_protected it will prevent the attributes from being set through mass-assignment, and you would spesifically need to use object.attribute = 'value' to set it. The value will not be removed from the SQL though, like it does during updates when you use attr_readonly, it will be set to nil. If this is not good enough you willl need to remove the attibute manually from the object before you create it.   before_create(:remove_attribute)

  private

  def remove_attribute     @attributes.delete('unwanted_attribute')   end

Beware that this removes the attribute completly from the current object. If you need to access the removed attribute after having created the record, the record will need to be reloaded.