【发布时间】:2018-02-16 21:10:51
【问题描述】:
我有一个来自Devise gem 的父类User:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :first_name, presence: true, length: { maximum: 256 }
validates :last_name, presence: true, length: { maximum: 256 }
def full_name
first_name + ' ' + last_name
end
end
我想在我的项目中拥有两种类型的帐户,例如Participant 和Mentor。只要我读过,使用单表继承(STI)是一个很好的方法,但是我新添加的字段并没有出现在模型中。
我有以下迁移文件:
class CreateParticipants < ActiveRecord::Migration[5.1]
def change
create_table :participants do |t|
t.string :team_name
t.references :mentor
t.timestamps
end
end
end
和
class CreateMentors < ActiveRecord::Migration[5.1]
def change
create_table :mentors do |t|
t.timestamps
end
end
end
简单地说,我想要一个Participant 模型,其中我有额外的team_name 字段以及一个与Mentor 模型的外键关系。
participant.rb:
class Participant < User
# _________________^
validates :team_name, presence: true, length: { maximum: 500 }
belongs_to :mentor
end
mentor.rb:
class Mentor < User
# ____________^
has_many :participants
end
但是新添加的字段没有出现,虽然出现在db/schema.rb中。
【问题讨论】:
标签: ruby-on-rails ruby database sti