My DB tables and field names are in CamelCase. Is it possible to convert those names into snake_case on a fly? To make model methods look pretty?
The app is JRubyOnRails 3.0 / MSSQL DB / ActiveRecord-JDBC-adapter.
@arkadiy,as a matter of fact, I was looking into this just this very day.
For table names, we of course have the set_table_name
method:
class CamelCasedFoo < ActiveRecord::Base
set_table_name :CamelCasedTable
end
For things like primary keys, we have set_primary_key
:
class CamelCasedBar < ActiveRecord::Base
...
set_primary_key "CamelCasedTableID"
end
And it should be possible to alias funky, legacy column names to something more rails-friendly with alias_attribute
:
class CamelCasedBaz < ActiveRecord::Base
...
alias_attribute :preferred_delivery, :DeliveryFrequency
end
One key thing to keep in mind is to watch out for any column names that are ruby or rails keywords or magic field names.
Rails appears to have all that metaprogramming goodness to allow you to work around legacy db table names and columns. You may wish to have a read of Jonathan Hui's blog post on "Ruby on Rails 3 Model Working with Legacy Database". And you might want to have a look at the safe_attributes gem.