Skip to content
Advertisement

How to force Rails ActiveRecord to use CamelCase in queries, instead of snake_case

I am trying to create a rails app that will connect to a production SQLServer that is used by 3rd party software, so migrating the database is not an option.

I am able to connect to the SQL server without any issues (I used this guide for connecting to existing database, and this one to connect rails to SQL Server), and I can query tables that use snake_case names.

The tables I need to access are in CameCase and I am not able to rename it because the 3rd party software will stop working.

I’ve already implemented /config/environment.rb

ActiveRecord::Base.pluralize_table_names = false to remove pluralization.

For example:

UserTable.count

ActiveRecord creates this: *SELECT COUNT(*) FROM [user_table]*

I want ActiveRecord to create this: SELECT COUNT(*) FROM [UserTable]

What file do I modify to specify the correct table name it should use?

Advertisement

Answer

I don’t think rails have any support as per your specification at base level. As rails believe in convention over configuration.

Although we can override the table name in your model.

user_table.rb

class UserTable < ApplicationRecord

  self.table_name = "UserTable"
  self.primary_key = "YourPrimaryKey"

end
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement