Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在相关模型中创建没有外键的配置文件_Ruby On Rails_Foreign Keys - Fatal编程技术网

Ruby on rails 在相关模型中创建没有外键的配置文件

Ruby on rails 在相关模型中创建没有外键的配置文件,ruby-on-rails,foreign-keys,Ruby On Rails,Foreign Keys,我在单个用户模型中定义了多个用户类型: enum role: { staff: 0, clinician: 1, admin: 2 } 临床医生用户都有一个临床医生配置文件,它是使用创建后自动创建的,我打算将临床医生配置文件id存储在users表中。出于某种原因,当创建临床医生配置文件时,包括临床医生用户在内的所有用户的用户表上的临床医生配置文件id保持为空。我该如何解决这个问题 模块临床医生用户 类临床医生档案

我在单个用户模型中定义了多个用户类型:

  enum role: { staff: 0, clinician: 1, admin: 2 }
临床医生用户都有一个临床医生配置文件,它是使用创建后自动创建的,我打算将临床医生配置文件id存储在users表中。出于某种原因,当创建临床医生配置文件时,包括临床医生用户在内的所有用户的用户表上的临床医生配置文件id保持为空。我该如何解决这个问题

模块临床医生用户

类临床医生档案<应用记录

用户表架构:


此处不应使用类方法对实例对象进行操作

您可以改为使用回调块:

after_create do |user|
  ClinicianProfile.create(user: user) if clinician?
end

此外,关联被定义为“属于”,因此父对象也可以创建关联,但这只是个人意见。

您可以添加“创建临床医生档案”方法吗?我没有明确的方法-它目前由临床医生关注模块中的这一行专门处理:创建后:create_clinician_profile,if::clinician?尝试显式创建方法def create_clinician_profile;self.clinician_profile.create!;endI在最初的问题中添加了我在临床医生关注点中创建的类方法-users表仍然具有临床医生配置文件id的所有空值
has_one :user, -> { where role: :clinician }
end
create_table "users", force: :cascade do |t|
    t.string   "email"
    t.datetime "created_at",                                       null: false
    t.datetime "updated_at",                                       null: false
    t.string   "password_digest"
    t.string   "remember_digest"
    t.string   "activation_digest"
    t.boolean  "activated",                        default: false
    t.datetime "activated_at"
    t.string   "reset_digest"
    t.datetime "reset_sent_at"
    t.string   "encrypted_password",   limit: 128
    t.string   "confirmation_token",   limit: 128
    t.string   "remember_token",       limit: 128
    t.datetime "confirmed_at"
    t.integer  "role",                             default: 0
    t.string   "first_name"
    t.string   "last_name"
    t.integer  "university_id"
    t.boolean  "approved",                         default: false
    t.integer  "market_id"
    t.integer  "clinician_profile_id"
    t.index ["clinician_profile_id"], name: "index_users_on_clinician_profile_id"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["market_id"], name: "index_users_on_market_id"
    t.index ["remember_token"], name: "index_users_on_remember_token"
after_create do |user|
  ClinicianProfile.create(user: user) if clinician?
end