2016-06-17 2 views
0

외래 키에 기본 이름이없는 경우 연결을 제대로 만들 수 없습니다. subjects에 속한 하나만 participant (foreign key = questioner_id)에 액세스하고 싶습니다.기본 열 이름을 사용하지 않을 때 많은 연결 오류가 발생했습니다.

subject_participants.participant_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" = ? 

인상? 그것은 단지 has_many 연관 일 뿐이므로이 경우 subject_participants 테이블을 호출해야한다고 생각하지 않습니다 ...

interested_idquestioner_id은 같은 모델이지만 같은 역할은 아닙니다. 하나는 subject_participants 테이블을 통해 가야하고, 다른 하나는 subjects 테이블

내 모델에서 직접 이동할 수 있습니다 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 

schem a.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 
+0

사실 나는 questioner_id (Participant 클래스)에 속한 모든 과목에 액세스하고 싶습니다. 나는'p.questioner'와 함께 당신의 방법을 시도했다. 나는 얻는다. 'Participant : 0x007f85781064e0> # – Orsay

+0

에 대한 정의되지 않은 메소드 질문자 왜 has_many라는 동일한 이름을 갖는 두 개의 연관을 가지고 있는가? –

+0

Participant는 subject를 게시 할 수 있으므로 (has_many : subjects가 있으므로) subject_participants 테이블을 사용하여 관심있는 주제에 적용 할 수 있습니다. 그것은 두 개의 다른 역할입니다. – Orsay

답변

1

변경

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

end 
+0

좋습니다. 오류가 없습니다. 그러나 요청은 좋은 것이 아닙니다. 내가 가진 'SELECT "주제". * FROM "주제"INNER JOIN "subject_participants" "주제". "id"= "subject_participants". "subject_id"WHERE "subject_participants". "interested_id"=? 주제 : [[ "interested_id는"1]] ' 그리고 난 당신이 같은 이름을 가진 두 단체가있는 협회 이름을 변경하시기 바랍니다 수 subject.questioner_id' – Orsay

+0

'에 요청을하고 싶은 당신은 has_many을 가질 수 있습니다 : : subject_interested, : : subject_participants, foreign_key : "관심있는", class_name : "참가자"#interested –

+0

participant.subjects를 수행 할 때마다 2 차 연결을 호출합니다. –

0

로 participant.rb 당신은 나에게 도움을, 감사 솔루션을 찾을 수 있도록 :

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 

될 수 있습니다. 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 
관련 문제