Does Rails generate a model from an existing table?

I am very new to the rails structure and want to know how to create a model based on an existing table. For example, I have a table called person and you want to create a model based on the columns from this table. However, when I use the "ruby script / generate model Person -skip-migration, it creates an empty table named people and creates a model after that. Is there a way to create a model after the table named person?

Thank.

+3
source share
1 answer

Rails is very self-confident, so if you have a table named "man" and you want the corresponding model to be called by Person, you need to tell Rails to explicitly not be so smart (otherwise it is supposed to look at the plural model name for table name).

class Person < ActiveRecord::Base
  set_table_name 'person'
end

If the primary key of your table is not called "id", you also need to specify this ...

set_primary_key 'person_id'

You may also need to specify a different auto-increment sequence name, depending on your database.

I don't know how I can automatically generate a model from an existing old table, but this should do you most of this way.

+15
source

Source: https://habr.com/ru/post/1726990/


All Articles