简体   繁体   中英

Why doesn't 'rails g scaffold' generate a db migration when invoked with a '--parent' argument?

I am attempting to subclass User in a Rails app (Rails 4.2.0, Devise 3.5.2)

If I invoke the generator with the --parent user argument, a database migration is not generated:

$ rails g scaffold participant token:string --parent user
      invoke  active_record
      create    app/models/participant.rb
      invoke    test_unit
      create      test/models/participant_test.rb
      create      test/fixtures/participants.yml
      invoke  resource_route
       route    resources :participants
      invoke  scaffold_controller
      create    app/controllers/participants_controller.rb
      invoke    haml
      create      app/views/participants
      create      app/views/participants/index.html.haml
      create      app/views/participants/edit.html.haml
      create      app/views/participants/show.html.haml
      create      app/views/participants/new.html.haml
      create      app/views/participants/_form.html.haml
      invoke    test_unit
      create      test/controllers/participants_controller_test.rb
      invoke    helper
      create      app/helpers/participants_helper.rb
      invoke      test_unit
      invoke    jbuilder
      create      app/views/participants/index.json.jbuilder
      create      app/views/participants/show.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/participants.coffee
      invoke    scss
      create      app/assets/stylesheets/participants.scss
      invoke  scss
   identical    app/assets/stylesheets/scaffolds.scss

Invoking the generator without --parent user causes the db migration to be generated (see the 3rd line):

$ rails g scaffold participant token:string
      invoke  active_record
      create    db/migrate/20160109231939_create_participants.rb
      create    app/models/participant.rb
      invoke    test_unit
      create      test/models/participant_test.rb
      create      test/fixtures/participants.yml
      invoke  resource_route
       route    resources :participants
      invoke  scaffold_controller
      create    app/controllers/participants_controller.rb
      invoke    haml
      create      app/views/participants
      create      app/views/participants/index.html.haml
      create      app/views/participants/edit.html.haml
      create      app/views/participants/show.html.haml
      create      app/views/participants/new.html.haml
      create      app/views/participants/_form.html.haml
      invoke    test_unit
      create      test/controllers/participants_controller_test.rb
      invoke    helper
      create      app/helpers/participants_helper.rb
      invoke      test_unit
      invoke    jbuilder
      create      app/views/participants/index.json.jbuilder
      create      app/views/participants/show.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/participants.coffee
      invoke    scss
      create      app/assets/stylesheets/participants.scss
      invoke  scss
   identical    app/assets/stylesheets/scaffolds.scss

Why does specifying a parent class disable the creation of a database migration? I cannot conceive of a legitimate use for a model that isn't persisted to the database in some fashion.

Adding a type field of type string to the Participant model to enable Single-Table Inheritance has no effect.

The --parent option is used in conjunction with single table inheritance.

I cannot conceive of a legitimate use for a model that isn't persisted to the database in some fashion

But that's exactly what STI does. It allows you to store multiple entities (models) in one single table.

Consider the model User stored in the table users . The table has a column is_admin .

You can have a separate model, Admin , that will share the users table but which will work only with the users having the is_admin flag set to true .

Initially you generate the User model

rails g model User name:string is_admin:boolean

Then you create the Admin model which will inherit from User and doesn't need it's own table

rails g model Admin --parent user

The --parent option assumes that you are already all setup for single table inheritance, ie the parent class has a table with a type column (or whatever column you are using for this).

Since the model will be stored in the parent's table, there is no need to create a new table for the subclass, hence no migration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM