有一个属于关联问题

时间:2017-09-05 19:46:50

标签: ruby-on-rails belongs-to has-one

我有一个具有单表继承和两个用户子类的用户模型:Staff和Clinician。为了避免用户表上的nil列,我创建了一个ClinicianProfile模型和clinician_profiles表。临床医生的名字在用户桌上。

class ClinicianProfile < ApplicationRecord
    belongs_to :clinician

class Clinician < User
  has_one :clinician_profile

似乎我应该可以在控制台中执行类似的操作:

ClinicianProfile.last.clinician.name

但是这给了我以下错误:

NameError: uninitialized constant ClinicianProfile::Clinician
但是,我可以这样做:

ClinicianProfile.last.clinician_id

我做错了什么?为什么我不能从用户表中访问临床医生的名字?

感谢您的帮助!

以下是架构:

create_table "users", force: :cascade do |t|
    t.string   "name"
    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.integer  "university_id"
    t.integer  "role"
    t.index ["email"], name: "index_users_on_email", unique: true
  end

  create_table "clinician_profiles", force: :cascade do |t|
    t.datetime "created_at",          null: false
    t.datetime "updated_at",          null: false
    t.string   "firstname"
    t.string   "lastname"
    t.string   "address1"
    t.string   "address2"
    t.string   "city"
    t.string   "state"
    t.string   "zip"
    t.boolean  "accepting_patients"
    t.integer  "rate"
    t.string   "license_number"
    t.string   "license_state"
    t.string   "school"
    t.integer  "year_graduated"
    t.boolean  "accepts_insurance"
    t.boolean  "sliding_scale"
    t.text     "bio"
    t.boolean  "verified"
    t.integer  "years_licensed"
    t.integer  "years_of_experience"
    t.integer  "clinician_id"
    t.integer  "years_practicing"
    t.string   "website"
  end

以下是控制台输出示例:

irb(main):001:0> ClinicianProfile.last
  ClinicianProfile Load (0.7ms)  SELECT  "clinician_profiles".* FROM "clinician_profiles" ORDER BY "clinician_profiles"."id" DESC LIMIT ?  [["LIMIT", 1]]
=> #<ClinicianProfile id: 1, created_at: "2017-08-26 17:09:44", updated_at: "2017-09-05 17:56:58", firstname: nil, lastname: nil, address1: "123 Fake ave", address2: "3233", city: "Fake", state: "ME", zip: "02412", accepting_patients: true, rate: 12, license_number: "1321132", license_state: "AR", school: "Brandeis", year_graduated: 1002, accepts_insurance: true, sliding_scale: true, bio: "mywebsite.com", verified: nil, years_licensed: 32, years_of_experience: nil, clinician_id: 2, years_practicing: 434, website: nil>
irb(main):002:0> ClinicianProfile.last.clinician.name
  ClinicianProfile Load (0.2ms)  SELECT  "clinician_profiles".* FROM "clinician_profiles" ORDER BY "clinician_profiles"."id" DESC LIMIT ?  [["LIMIT", 1]]
NameError: uninitialized constant ClinicianProfile::Clinician
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/inheritance.rb:152:in `compute_type'
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/reflection.rb:354:in `compute_class'
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/reflection.rb:350:in `klass'
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/associations/association.rb:118:in `klass'
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/associations/belongs_to_association.rb:55:in `find_target?'
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/associations/association.rb:138:in `load_target'
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/associations/association.rb:53:in `reload'
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/associations/singular_association.rb:14:in `reader'
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/associations/builder/association.rb:111:in `clinician'
    from (irb):2
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-5.0.1/lib/rails/commands/console.rb:65:in `start'
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-5.0.1/lib/rails/commands/console_helper.rb:9:in `start'
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-5.0.1/lib/rails/commands.rb:18:in `<top (required)>'
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require'
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `block in require'
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require'
    from /Users/michaelbaker/workspace/sample_a/bin/rails:9:in `<top (required)>'
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `load'
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `block in load'
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `load'
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Users/michaelbaker/.rbenv/versions/2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:55:in `require'

1 个答案:

答案 0 :(得分:0)

对于反高潮很抱歉,但这似乎没有对代码进行任何修改就解决了。