【发布时间】:2016-06-17 09:28:38
【问题描述】:
当外键没有默认名称时,我无法建立良好的关联。
我想访问属于一个participant的所有subjects(外键=questioner_id)。
这引发了我一个错误
p = Participant.first
p.subjects
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: subject_participants.participant_id: SELECT "participants".* FROM "participants" INNER JOIN "subject_participants" ON "participants"."id" = "subject_participants"."subject_id" WHERE "subject_participants"."participant_id" = ?
为什么它会寻找 subject_participants.participant_id ?这只是一个has_many关联,我认为这种情况下不应该调用subject_participants表...
interested_id 和questioner_id 来自同一个模型,但角色不同。一个必须通过subject_participants table,另一个必须直接进入subjects table
我的模型: 参与者.rb
class Participant < ActiveRecord::Base
has_many :subjects, foreign_key: "questioner_id", class_name: "Participant" #questioner
has_many :subjects, through: :subject_participants, foreign_key: "interested", class_name: "Participant" #interested
has_many :subject_participants
has_many :conference_participants
has_many :conferences, through: :conference_participants
end
主题.rb
class Subject < ActiveRecord::Base
validates_presence_of :title, :questioner, :conference, :description
has_many :subject_participants
has_many :interested, through: :subject_participants, :class_name => "Participant" #interested
belongs_to :questioner, :class_name => "Participant"
belongs_to :conference
end
subject_participant.rb
class SubjectParticipant < ActiveRecord::Base
validates_presence_of :interested_id, :subject_id
belongs_to :interested, :class_name => "Participant"
belongs_to :subject
end
schema.rb
create_table "participants", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
end
add_index "participants", ["email"], name: "index_participants_on_email", unique: true
add_index "participants", ["reset_password_token"], name: "index_participants_on_reset_password_token", unique: true
create_table "subject_participants", force: :cascade do |t|
t.integer "interested_id"
t.integer "subject_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "subjects", force: :cascade do |t|
t.string "title", null: false
t.text "description"
t.integer "questioner_id", null: false
t.integer "conference_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
【问题讨论】:
-
实际上我想访问属于 questioner_id(参与者类)的所有主题。我试着用
p.questioner做你的事情,我得到undefined method questioner for #<Participant:0x007f85781064e0> -
为什么会有两个同名的关联has_many:subjects。
-
因为参与者可以发布主题(所以他们有
has_many :subjects),但他们也可以申请感兴趣的主题,这要归功于表subject_participants。这是 2 个不同的角色 -
好的,当你参与者.subjects 将调用哪个关联时,我认为你可以为这些关联使用不同的名称。
标签: ruby-on-rails ruby has-many-through model-associations