Ludivinecp Ludivinecp - 5 months ago 17
Ruby Question

Has many associations error when not using default column name

I cannot make good associations when the foreign key has not the default name.
I would like to access to all

subjects
which belongs_to one
participant
(foreign key =
questioner_id
).

It raise me an error

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" = ?


Why does it looks for
subject_participants.participant_id
? It's just a has_many association, I don't think that
subject_participants
table should be called in this case...

interested_id
and
questioner_id
are from the same model but not the same role. One has to go through
subject_participants
table and the other has to go directly in
subjects
table

My models :
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


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

Answer

Change your participant.rb to

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

end