Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to re-name a ActiveRecord Model which can automatically change the table name in DB?

I have a Active Record model "car", I would like to change the name of this model to "train" without changing functionalities inside, that's only change the name. Also, the table name should be changed to "trains".

Is there any rails command can do that at onece? Or I have to manually change the name in side the class or migration? If I have to change manually, it's gonna be complicated, because I have to also change other models which have associations to my "car" model.

Any good suggestions?

like image 459
Mellon Avatar asked Jan 28 '11 09:01

Mellon


People also ask

How do I change the model name in Ruby?

If you're using RubyMine, go into the model, right click the class name > refactor and change the name. RubyMine will refactor everything and create a new migration for the database as well.

What is ActiveRecord naming convention?

Active Record uses naming conventions for the columns in database tables, depending on the purpose of these columns. Foreign keys - These fields should be named following the pattern singularized_table_name_id (e.g., item_id , order_id ).

How do you change the name of a column in Ruby?

If you happen to have a whole bunch of columns to rename, or something that would have required repeating the table name over and over again: rename_column :table_name, :old_column1, :new_column1 rename_column :table_name, :old_column2, :new_column2 ...

What is ORM in ROR?

ORM is Object Relational Mapper. It means you don't have to manually call the database yourself; the ORM handles it for you. Ruby on Rails uses one called ActiveRecord, and it's a really good one. ORM allows you to do things such as: User.


2 Answers

I figured out the following way:

1, generate migration file:

rails generate migration rename_cars_to_trains
  1. edit the created migration file to:

    class RenameCarsToTrains < ActiveRecord::Migration
      def self.up
        rename_table :cars, :trains
      end
    
      def self.down
        rename_table :trains, :cars
      end
    end
    
  2. rake db:migrate

After these steps, the table name changed from cars to trains, then, I have to manually change the controller and views names and the associations...

If you have any more efficient way, let me know...

like image 101
Mellon Avatar answered Nov 08 '22 09:11

Mellon


I would recommend the following:

  1. Change manually the Active Record model class into Train

  2. Make migration to change the database table name from cars to trains

  3. Make good search to change references from Car to Train.

If you have constantly the need to change database table names, you might want to reconsider naming the tables more abstact way. Like in this case you could have table called vehicles and have the "type" field specifying the type (for instance: car or train).

like image 22
hade Avatar answered Nov 08 '22 11:11

hade