Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails attribute names camelcase issue

I have legacy Sql Server database and Rails applications. Column names in database are all in PascalCase (FirstName, CustomerId...).

I'm searching for an easy way to use underscore_casing in Ruby and PascalCasing in database. I would like to write first_name in Rails for FirstName column in database.

camelize and underscore will convert a string to required casing

but I'm looking for something more general like: ActiveRecord::Base.camelize_table_column_names = true

like existing ActiveRecord::Base.pluralize_table_names

Just like translating Ruby in rjs templates to JavaScript cameCase convention.

like image 205
JongHyun Lee Avatar asked Oct 29 '25 09:10

JongHyun Lee


1 Answers

Maybe set up aliases for all defined column names?

column_names.each do |column_name|
  alias_attribute column_name.underscore, column_name
end

You could add that to a concern and include that concern in all relevant models.

like image 174
Michael Kohl Avatar answered Oct 31 '25 01:10

Michael Kohl