0
votes

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
2
Actually I would like to access to all the subjects wich belongs to the questioner_id (Participant class). I tried to do your way with p.questioner and i get undefined method questioner for #<Participant:0x007f85781064e0>Orsay
why do you have two associations with same name has_many: subjects.Asnad Atta
Because Participant can post subjects (so they have has_many :subjects), but they can also apply to a subject as interested, thanks to the table subject_participants. It's 2 differents rolesOrsay
okay when you participant.subjects which association will get called I think you can use different names for these associations.Asnad Atta

2 Answers

1
votes

Change your participant.rb to

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

end
0
votes

You make me find the solution, thanks for help :

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