How to create a model of an existing database? rails

1

Hello, my question is this,

I'm doing an application in rails, but I already have the databases created, one of the tables for example is called "UsersMaster" and I can not change its name.

Now, how do I create a rails model for that table in order to use the ActiveRecord?

    
asked by Angel Diaz 22.09.2016 в 19:01
source

1 answer

1

I think that for these types of tasks it is always better to create the models by hand, something like:

class NuevoModelo < ActiveRecord::Base
  set_table_name "nombreDeTablaRaro"
  set_primary_key "IDQueNoSigueConvencionRails"
end

But if you do not want to give it to you, you could try using rmre that uses reverse engineering to generate the models. It has been without maintenance for a long time, but neither has the basic structure of a rails 3 model changed to what is today 5 rails.

After installing the gem with gem install rmre , you start the process with

rmre -a mysql2 -d basededatos -u usuario -p contraseña -o /ruta/donde/crear/modelos'

Obviously mysql2 can be changed by the adapter you are using for your specific engine. Anyway, do not trust 100% of what you generate and adapt it to your own needs or naming conventions that you want to use for your models.

    
answered by 23.09.2016 в 13:22