0

I am trying to use SQL Server with ruby on rails. I created a test table and it did not work until I made the table and field names all lower case. The database I wish to use is quite large and if I made everything lowercase it would be terrible as far as readability goes. Someone suggested to use 'lowercase_schema_reflection' but they told me to add it to my config/initializers file for the adapter. I do not have one so if anyone know how to implement this I would appreciate it if they could tell me.

Solution: Steve found the solution in the comments and did not make a post so I will provide the solution here.

He pointed out that I needed to do a rake db:schema:dump. Although it still did not work properly. The Table in the database is "EMP". The rake command calls it "emp". So in the model the line "self.table_name = 'EMP'", as accurate as it is, still fails. It needs to be "self.table_name = 'emp'". I am truly surprised a modern framework is case-sensitive (a mistake in any programming language design) let alone be inconsistent in its case-sensitivity. This error in design has cost me many hours and I am sure I am not the only one. Lets hope any developers designing any language and / or frameworks in the future avoid this costly mistake.

Joe
  • 379
  • 1
  • 6
  • 21
  • Possible duplicate of http://stackoverflow.com/questions/4613574/how-do-i-explicitly-specify-a-models-table-name-mapping-in-rails. Essentially, you can follow Rails conventions in your codebase and then add `self.table_name = ...` to link your code to your actual database table names. – steve klein Jul 16 '15 at 20:59
  • Tried that. It does not work. – Joe Jul 17 '15 at 15:03
  • Not sure what didn't work but if you want to use the option you mentioned in your email, just create a new file in `config/initializers` (e.g. `active_record.rb`) and put in this line of code: `ActiveRecord::ConnectionAdapters::SQLServerAdapter.lowercase_schema_reflection = true`. Restart Rails, run any migrations and you should be good to go. – steve klein Jul 17 '15 at 17:51
  • What did not work was getting data from a table. All I am trying to do is test the data connection and it fails. I do not have any migrations, all the data and schema already exists in the database. self.table_name is being ignored as well. – Joe Jul 17 '15 at 19:07
  • Did you try the initializer? If so, what did you specifically access and what response did you get? I would try to access using Rails console initially. – steve klein Jul 17 '15 at 19:24
  • I did use the console. Thats what produced the error message. I put this code in config\initializers\active_record.rb: ActiveRecord::ConnectionAdapters::SQLServerAdapter.lowercase_schema_reflection = true ActiveRecord::Base.table_name_prefix = 'dbo.' Tried it with or without the second line. Still fails. Its too bad because this seems like a good framework. – Joe Jul 17 '15 at 19:37
  • What error do you get? – steve klein Jul 17 '15 at 19:42
  • Sorry, thought I posted it. Employee(Table doesn't exist) – Joe Jul 17 '15 at 19:46
  • I have never heard of Rails ActiveRecord working without migrations. I thought they were required. – steve klein Jul 17 '15 at 19:52
  • What is a migration according to ruby? Everytime I have done a migration it included moving data from one database to another. I have an existing database and nothing to migrate unless ruby uses an unconventional use of the word. I am looking at this http://edgeguides.rubyonrails.org/active_record_migrations.html but all the examples are modifying something. I do'nt have anything to modify. Do I have to create a table just so I can run a modification? Do I just make up any kind of change? – Joe Jul 17 '15 at 20:07
  • 1
    Did you see [this question](http://stackoverflow.com/questions/4597570/putting-rails-over-top-of-an-existing-database)? Maybe it will help you. In Rails, migrations are used for database structure as often (or more frequently) than database data. I would recommend you use `rake db:schema:dump` to generate a schema file which will serve to synchronize your Rails components (models, views, controllers, helpers, etc) with your actual DB schema. Then, use Rails migrations going forward for any schema changes. – steve klein Jul 17 '15 at 20:35
  • Thanks Steve!! That was something I needed. I posted the complete solution above. – Joe Jul 20 '15 at 14:02
  • Glad I was able to help @Joe. – steve klein Jul 20 '15 at 14:02

0 Answers0