I am looking for a solution to the following problem: I have an ActiveRecord object that is supported by an updated database view (in DB2 using the activerecord-jdbc-adapter harness). This view contains one column, which is calculated from other columns and is read-only: you cannot set this column in any real way. When a new record is created for this object, this field should not be set. However, by default, ActiveRecord sets it with a "default" (NULL), which is rejected by the database.
attr_readonly is not a solution, as it only excludes columns from updates, not creates.
attr_ignore, for example, implemented by the Lincoln pearl, is also not a solution, because then the field is completely ignored. However, the column still needs to be read and accessible. It is actually even used as part of a relationship.
There are ways to prevent the setting of a specific attribute of an ActiveRecord object, but usually this does not prevent the inclusion of this attribute in create or update statements.
Does anyone know if there is a way in ActiveRecord to specify a column as "never set this field"?
Update in response to Arsen7: I tried using hook after initialization to remove the attribute from the newly created object, so it is not included in the built SQL. The problem is that the attribute is completely removed and inaccessible at all, largely identical to the igonre_attr situation described above. Due to caching, this is not trivial to work around and will require additional logic to force the reloading of the entities of these specific tables. This can probably be achieved by overriding createto add a "reload" in addition to using the after_initialize parameter.
(As Arsen7 noted, I forgot to mention that I am in ActiveRecord 3.0.9)
My decision
ActiveRecord::Base, before_create after_create. hook before_create "" @attributes . hook after_create "" , , .
create, , Arsen7 .