# Activerecord readonly attributes

**URL:** <https://www.ruby-forum.com/t/activerecord-readonly-attributes/187048>\
**Category:** Rails\
**Created:** [April 16, 2010, 6:44am UTC](https://www.ruby-forum.com/t/activerecord-readonly-attributes/187048 "2010-04-16T06:44:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [April 16, 2010, 6:44am UTC](https://www.ruby-forum.com/t/activerecord-readonly-attributes/187048/1 "2010-04-16T06:44:32Z")

</div>

I do not know why my previous post was deleted. I am trying again.

In Firebird database one can declare computed columns. These columns  
are readonly in the database. How do I stop activerecord to include  
these columns in insert statements when creating a record?

I know about attr\_readonly which works fine when updating.

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [April 16, 2010, 7:16am UTC](https://www.ruby-forum.com/t/activerecord-readonly-attributes/187048/2 "2010-04-16T07:16:10Z")

</div>

I hope attr\_accessor can work for this purpose. It wont be inserted in  
db.

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [April 16, 2010, 8:00am UTC](https://www.ruby-forum.com/t/activerecord-readonly-attributes/187048/3 "2010-04-16T08:00:06Z")

</div>

Can you please explain this? I do not see anything about attr\_accessor  
in the activerecord api.

Thanks

Op 16/04/2010 07:14, K.Arunmohan het geskryf:

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [April 16, 2010, 12:15pm UTC](https://www.ruby-forum.com/t/activerecord-readonly-attributes/187048/4 "2010-04-16T12:15:22Z")

</div>

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.
