2017-09-05 44 views
0

저는 하나의 테이블 상속과 두 개의 사용자 하위 클래스 인 직원 및 임상의가있는 사용자 모델을 가지고 있습니다. 사용자 테이블에서 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' 
+0

'belongs_to : clinician, class_name : Clinician "' –

+0

응답 해 주셔서 감사합니다.이 문제를 해결하지 못했습니다. – mike9182

+0

어디에서'NameError : 초기화되지 않은 상수 ClinicianProfile :: Clinician' 오류가 발생했는지, 레일 서버 출력을 질문에 추가 할 수 있습니까? –

답변

0

죄송하지만,이 코드에 대한 수정없이 겉으로는 해결.