定义Sti表的关联

发布于 2025-02-03 08:56:34 字数 598 浏览 4 评论 0原文

,并且有三个表从此表继承为:

class Manager < User
  has_many :projects
end
class Qa < User
  has_many :bugs
end
class Developer < User
  has_many :bugs
  has_and_belongs_to_many :projects
end

project and bug表as:

class Bug < ApplicationRecord
  belongs_to :developer
  belongs_to :qa
  belongs_to :project
end
class Project < ApplicationRecord
  belongs_to :manager
  has_many :bugs
  has_and_belongs_to_many :developers
end

我有一个表用户 在数据库中,我正在使用STI用于Manager,QA和开发人员,但是如何定义与这三个表相对应的迁移?

I have a table User and there are three tables that inherit from this table as:

class Manager < User
  has_many :projects
end
class Qa < User
  has_many :bugs
end
class Developer < User
  has_many :bugs
  has_and_belongs_to_many :projects
end

The project and bug tables are as:

class Bug < ApplicationRecord
  belongs_to :developer
  belongs_to :qa
  belongs_to :project
end
class Project < ApplicationRecord
  belongs_to :manager
  has_many :bugs
  has_and_belongs_to_many :developers
end

The User table exists in the database and I am using STI for Manager, QA and Developer but how do I define migrations corresponding to the associations of these three tables?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青朷 2025-02-10 08:56:34

因此,我最终最终使用了STI。数据库迁移如下:

class DeviseCreateUsers < ActiveRecord::Migration[5.2]
  def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ''
      t.string :encrypted_password, null: false, default: ''

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      # t.integer  :sign_in_count, default: 0, null: false
      # t.datetime :current_sign_in_at
      # t.datetime :last_sign_in_at
      # t.inet     :current_sign_in_ip
      # t.inet     :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at

      t.string :type, default: 'Developer'
      t.string :name

      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end
class CreateBugs < ActiveRecord::Migration[5.2]
  def change
    create_table :bugs do |t|
      t.string :title
      t.datetime :deadline
      t.string :kind
      t.string :stature
      t.text :description

      t.belongs_to :developer, index: false, null: true, default: nil
      t.belongs_to :qa, index: true
      t.belongs_to :project, index: true

      t.timestamps
    end
  end
end
class CreateDevelopersProjects < ActiveRecord::Migration[5.2]
  def change
    create_table :projects do |t|
      t.string :name
      t.belongs_to :manager, index: true

      t.timestamps
    end
    create_table :projects_users, id: false do |t|
      t.belongs_to :developer, index: true
      t.belongs_to :project, index: true

      t.timestamps
    end
    add_index :projects, :name, unique: true
  end
end

So, I ultimately ended up using STI. The database migrations are as under:

class DeviseCreateUsers < ActiveRecord::Migration[5.2]
  def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ''
      t.string :encrypted_password, null: false, default: ''

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      # t.integer  :sign_in_count, default: 0, null: false
      # t.datetime :current_sign_in_at
      # t.datetime :last_sign_in_at
      # t.inet     :current_sign_in_ip
      # t.inet     :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at

      t.string :type, default: 'Developer'
      t.string :name

      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end
class CreateBugs < ActiveRecord::Migration[5.2]
  def change
    create_table :bugs do |t|
      t.string :title
      t.datetime :deadline
      t.string :kind
      t.string :stature
      t.text :description

      t.belongs_to :developer, index: false, null: true, default: nil
      t.belongs_to :qa, index: true
      t.belongs_to :project, index: true

      t.timestamps
    end
  end
end
class CreateDevelopersProjects < ActiveRecord::Migration[5.2]
  def change
    create_table :projects do |t|
      t.string :name
      t.belongs_to :manager, index: true

      t.timestamps
    end
    create_table :projects_users, id: false do |t|
      t.belongs_to :developer, index: true
      t.belongs_to :project, index: true

      t.timestamps
    end
    add_index :projects, :name, unique: true
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文