不使用默认列名时有很多关联错误

Has many associations error when not using default column name

当外键没有默认名称时,我无法很好地关联。 我想访问所有 subjects 其中 belongs_to 一个 participant (外键 = 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 table...

interested_idquestioner_id来自同一个模型但不是同一个角色。一个要经过subject_participants table 一个要直接在subjects table

我的模特: participant.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

subject.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

主题_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

将您的 participant.rb 更改为

class Participant < ActiveRecord::Base
  .....
  has_many :subject_participants,class_name: "SubjectParticipant",  foreign_key: "interested_id"

end

你让我找到了解决办法,谢谢你的帮助:

participant.rb

class Participant < ActiveRecord::Base
  has_many :subject_participants, class_name: "SubjectParticipant", foreign_key: "interested_id"
  has_many :subjects_interested_in, through: :subject_participants, :source => "subject"
  has_many :subjects, foreign_key: "questioner_id"
  has_many :conference_participants
  has_many :conferences, through: :conference_participants
end

subject.rb

class Subject < ActiveRecord::Base
  validates_presence_of  :title, :questioner, :conference, :description

   has_many :subject_participants
   has_many :interested, through: :subject_participants #interested
   belongs_to :questioner, class_name: "Participant"
   belongs_to :conference
end