Interesting
- attr_readonly marks an attribute as, ah, read only — use it to tell ActiveRecord that an attribute should not be a part of update operations. Rails uses attr_readonly internally with counter caches (search for “counter_cache” under ActiveRecord::Associations::ClassMethods) since counter caches are incremented/decremented directly in the database with sql. Without attr_readonly, subsequent updates of the counter_cache’d model would revert the counter to the value of the counter at the time the model was loaded.
Note: attr_readonly was either buggy or not exposed prior to 1.2.3. If you are using a version of rails prior to 1.2.3 you can do this instead:
def attributes_with_quotes(include_primary_key = true)
attributes.inject({}) do |quoted, (name, value)|
if column = column_for_attribute(name)
# original:
# quoted[name] = quote_value(value, column) unless !include_primary_key && column.primary
quoted[name] = quote_value(value, column) unless !include_primary_key &&
(column.primary || ["your_attributes", "listed_here"].include?(column.name))
end
quoted
end
end
Ask for Help
- help: Looking for recommendations on converting an existing schema to a new schema. We are considering dumping the existing schema to yaml (using ar_fixtures) and making the transformations there.
- answer #1: One recent project had a “liberate” script that extracted information from the legacy database via sql statements and constructed AR model objects as necessary. The liberate script grew to some 1500 lines of code and was refactored many times.
- answer #2: Another project did the data migration by first importing the legacy database and then using rails migrations as needed to transform the data to the new schema. Most of the migrations used sql for the transformations. These migrations did not have associated unit tests.
- Please offer your suggestions in the comments.