为什么用“ --parent”参数调用时“ rails g scaffold”不会生成数据库迁移? [英] Why doesn't 'rails g scaffold' generate a db migration when invoked with a '--parent' argument?

查看:39
本文介绍了为什么用“ --parent”参数调用时“ rails g scaffold”不会生成数据库迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Rails应用中将 User 子类化(Rails 4.2.0,Devise 3.5.2)

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.

添加类型字段,以启用单表继承

推荐答案

-parent 选项假定您已经为单表继承设置了全部,即父类的表具有 type 列(或用于此目的的任何列)。

The --parent option assumes that you are already all setup for single table inheritance, i.e. 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.

这篇关于为什么用“ --parent”参数调用时“ rails g scaffold”不会生成数据库迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆