Has One Belongs To Association Issue

68 views Asked by At

I have a User Model with single table inheritance and two user sub classes: Staff and Clinician. To avoid nil columns on the user table I created a ClinicianProfile model and clinician_profiles table. The clinician's name is on the user table.

class ClinicianProfile < ApplicationRecord
    belongs_to :clinician

class Clinician < User
  has_one :clinician_profile

It seems that I should be able to do something like this in console:

ClinicianProfile.last.clinician.name

but this gives me the following error:

NameError: uninitialized constant ClinicianProfile::Clinician

I can, however, do:

ClinicianProfile.last.clinician_id

What am I doing wrong? Why can't I access the name of the clinician from the users table?

Thanks for any help!

Here are the schema:

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

Here is example console output:

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

There are 1 answers

0
mike9182 On

Sorry for the anti-climax, but this resolved seemingly without any modifications to the code.