Tyler DeWitt
Tyler DeWitt

Reputation: 23576

Define table name Ruby on Rails 3.2.1

I've got a legacy database that I'm connecting to via the sqlserver adapter. One of the databases is called "SiteIndex_Players". I've generated the following model:

class SiteIndexPlayer < ActiveRecord::Base
  set_table_name = "SiteIndex_Players"
end

I've also tried:

class SiteIndexPlayer < ActiveRecord::Base
  table_name = "SiteIndex_Players"
end

Both ways, when I run the rails console rails c, I get this:

1.9.2-p290 :001 > SiteIndexPlayer.first
SiteIndexPlayer Load (352.1ms)  EXEC sp_executesql N'SELECT TOP (1) [site_index_players].*    FROM [site_index_players]'
ActiveRecord::StatementInvalid: TinyTds::Error: Invalid object name 'site_index_players'.: EXEC sp_executesql N'SELECT TOP (1) [site_index_players].* FROM [site_index_players]'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-sqlserver-adapter-3.2.1/lib/active_record/connection_adapters/sqlserver/database_statements.rb:412:in `each'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-sqlserver-adapter-3.2.1/lib/active_record/connection_adapters/sqlserver/database_statements.rb:412:in `handle_to_names_and_values_dblib'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-sqlserver-adapter-3.2.1/lib/active_record/connection_adapters/sqlserver/database_statements.rb:401:in `handle_to_names_and_values'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-sqlserver-adapter-3.2.1/lib/active_record/connection_adapters/sqlserver/database_statements.rb:373:in `_raw_select'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-sqlserver-adapter-3.2.1/lib/active_record/connection_adapters/sqlserver/database_statements.rb:367:in `block in raw_select'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-sqlserver-adapter-3.2.1/lib/active_record/connection_adapters/sqlserver/database_statements.rb:367:in `raw_select'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-sqlserver-adapter-3.2.1/lib/active_record/connection_adapters/sqlserver/database_statements.rb:350:in `do_exec_query'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-sqlserver-adapter-3.2.1/lib/active_record/connection_adapters/sqlserver/database_statements.rb:24:in `exec_query'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-sqlserver-adapter-3.2.1/lib/active_record/connection_adapters/sqlserver/database_statements.rb:293:in `select'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:16:in `select_all'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/querying.rb:38:in `block in find_by_sql'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/explain.rb:40:in `logging_query_plan'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/querying.rb:37:in `find_by_sql'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/relation.rb:170:in `exec_queries'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/relation.rb:159:in `block in to_a'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/explain.rb:33:in `logging_query_plan'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/relation.rb:158:in `to_a'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/relation/finder_methods.rb:377:in `find_first'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/relation/finder_methods.rb:122:in `first'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/querying.rb:5:in `first'
from (irb):1
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start'
from /home/ubuntu/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'1.9.2-p290 :002 > 

Makes sense, the table is "SiteIndex_Players", not "site_index_players" (notice the missing underscore).

Is there a way I can define the table to have the name "SiteIndex_Players"?

Thanks

Upvotes: 0

Views: 1188

Answers (4)

tobinjim
tobinjim

Reputation: 1852

According to the Rails 4.0 documentation you now use the self.table_name = "foo" format.

class SiteIndexPlayer < ActiveRecord::Base
  self.table_name = "SiteIndex_Players"
end

Upvotes: 0

3.141592
3.141592

Reputation: 106

since Rails 3.2

set_table_name "the_name"    

was deprecated. Now one should use

self.table_name = "the_name"    

instead.

Upvotes: 2

Carlos Ramirez III
Carlos Ramirez III

Reputation: 7434

Instead of

set_table_name = "SiteIndex_Players"

you need

set_table_name "SiteIndex_Players"

without the equals sign.

See: http://apidock.com/rails/ActiveRecord/Base/set_table_name/class

Upvotes: 1

iltempo
iltempo

Reputation: 16012

Leave out the equal sign:

class SiteIndexPlayer < ActiveRecord::Base
  set_table_name "SiteIndex_Players"
end

Upvotes: 0

Related Questions