activerecord - Custom Named Attributes in Rails -


Whether it is possible to customize / override the name of an attribute in ActiveRecord, so that it does not match the column name in the database. ?

In my specific case there is a legacy column, "amendment", that I can not remove at this time, the name of the column conflicts with actions_as_audited. Until I complete my migration, which of the necessary legacy code I have for those errors?

The solution I want to override the attribute name for this column, and some areas that call it need to be updated. Thus, to allow legacy columns living with law_a_audited.

I have acts_as_audited , but I'm assuming that its implementation is that column For the accelerator being overridden. In that case, you should be able to do just that:

  class ActiveRecord :: base def self.name_column (column_name, new_name) define_method (new_name) {read_attribute column_name} define_method ("# {New_name} = ") {| Price | Write_attribute column_name, value} define_method ("# {new_name}?") {Attribute_present? Column_name} end end  

These overrides will reach the column called column_name without going through the accessor.

Oh, bonus duplication - destroying metaprogramming response:

class ActiveRecord :: Base def self.name_column (column_name, new_name) {'' = & gt; : Read_attribute, '=' = & gt; : Write_attribute, '?' = & Gt; Attribute_present? } .each do | Suffix, method. Define_method ("# {new_name} # {suffix}") {| * Args | Only because I can show how it can be done.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -