How do I explicitly specify a Model's table-name mapping in Rails?

Ruby on-Rails

Ruby on-Rails Problem Overview


I have a Model class called Countries and I want it to map to a DB table called 'cc'.

How is that done in Rails?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Rails >= 3.2 (including Rails 4+ and 5+):
class Countries < ActiveRecord::Base
  self.table_name = "cc"
end
Rails <= 3.1:
class Countries < ActiveRecord::Base
  self.set_table_name "cc"
  ...
end

Solution 2 - Ruby on-Rails

class Countries < ActiveRecord::Base
    self.table_name = "cc"
end

In Rails 3.x this is the way to specify the table name.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEran KampfView Question on Stackoverflow
Solution 1 - Ruby on-RailsZabbaView Answer on Stackoverflow
Solution 2 - Ruby on-RailschelofmView Answer on Stackoverflow