Имеет отношение к проблеме ассоциации
У меня есть Модель пользователя с наследованием одной таблицы и двумя пользовательскими подклассами: Персонал и Клиницист. Чтобы избежать нуля столбцов в пользовательской таблице, я создал модель 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 ответ
Извините за анти-кульминацию, но это, похоже, решено без каких-либо изменений в коде.