1

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?

User9123
  • 55
  • 7
  • 1
    You could look at `self.table_name = 'UserTable'` inside the `UserTable` model class and not worry about the `pluralize_table_names` setting; then use [`alias_attribute`](https://api.rubyonrails.org/classes/ActiveModel/AttributeMethods/ClassMethods.html#method-i-alias_attribute) to deal with the column names. – mu is too short Aug 09 '20 at 18:14

1 Answers1

2

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
Ravi Teja Gadi
  • 1,755
  • 2
  • 8
  • 15
  • Followup question, if I do this in the console> UserTable.first I get this: UserTable Load (9.4ms) EXEC sp_executesql N'SELECT [UserTable].* FROM [UserTable] OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY', N'@0 int', @0 = 1 [["LIMIT", nil]] ActiveRecord::StatementInvalid: TinyTds::Error: Incorrect syntax near '0'.: EXEC sp_executesql N'SELECT [UserTable].* FROM [UserTable] OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY', N'@0 int', @0 = 1 from (irb):10 Any Idea? – User9123 Aug 09 '20 at 18:53
  • @User9123 may I know which database software you are using – Ravi Teja Gadi Aug 09 '20 at 18:56
  • @User9123 I have replicated your use case in postgresql and it's working fine. I'm sure it will also works in MySQL. If you are using any database other than this. Please edit your question with your model code. – Ravi Teja Gadi Aug 09 '20 at 19:02
  • I am using SQL Server 2012, i've posted more info here https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/issues/843 – User9123 Aug 09 '20 at 19:23
  • willing to compensate you for assistance Ravi. email me: aet08 hotmail – User9123 Aug 09 '20 at 19:24
  • Yeah... Just saw the GitHub link. I think you need to specify the primary key in the model or else it will take 'id' as primary key by default – Ravi Teja Gadi Aug 09 '20 at 19:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/219507/discussion-between-user9123-and-ravi-teja-gadi). – User9123 Aug 09 '20 at 19:30