ruby on rails migrations using an existing table

I have this problem. I need to use an existing table on a mysql database. The name of the table is not compatible with RoR conventions and I need to remap the table name and the name of the attributes. I have created a scaffold to visualize on a web page the content of the table but I can't change the mapping. Is there a solution to indicate to RoR the relation between the name of the class and the name of the table in the database? and a solution to indicate the relation between the attribute of the class and field on the table? Thanks.

Upvotes: 0

Views: 84

Answers (2)

Igor Drozdov
Igor Drozdov

Reputation: 15045

The table name can be specified using table_name class method.

For the attributes/column, you need to explicitly specify aliases for the attributes using alias_attribute method. For example, if you have name_of_thing column, but want to treat it as name, then you need something like this in your model:

class CreateUtenti < ActiveRecord::Base
  self.table_name = "another_name"
  alias_attribute :name, :name_of_thing
end

Upvotes: 1

Kaushlendra Tomar
Kaushlendra Tomar

Reputation: 1440

Yes you can pass table name in model like:

class YourModel < ActiveRecord::Base
  self.table_name = "pass_table_name_here"
end

Upvotes: 0

Related Questions