簡體   English   中英

為什么用“ --parent”參數調用時,“ rails g scaffold”不會生成數據庫遷移?

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

我正在嘗試在Rails應用中將User子類化(Rails 4.2.0,Devise 3.5.2)

如果我使用--parent user參數調用生成器,則不會生成數據庫遷移:

$ 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

在沒有--parent user情況下調用生成器會導致生成數據庫遷移(請參見第三行):

$ 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

為什么指定父類會禁止創建數據庫遷移? 我無法想到某種無法以某種方式保留到數據庫的模型的合法使用。

將字符串type字段添加到參與者模型以啟用單表繼承無效。

--parent選項與單表繼承結合使用。

我無法想到某種無法以某種方式持久化到數據庫的模型的合法使用

但這正是STI所做的。 它使您可以在一個表中存儲多個實體(模型)。

考慮將模型User存儲在表users中 該表具有一列is_admin

您可以擁有一個單獨的模型Admin ,該模型將共享users表,但僅適用於is_admin標志設置為true

最初,您生成User模型

rails g model User name:string is_admin:boolean

然后,創建將繼承自User且不需要它自己的表的Admin模型

rails g model Admin --parent user

--parent選項假定您已經為單一表繼承做好了准備,即父類的表具有type列(或您為此使用的任何列)。

由於模型將存儲在父表中,因此無需為子類創建新表,因此無需遷移。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM