조인 된 테이블의 조건을 지정하는 ActiveRecord가있는 레일에서 쿼리를 수행하려고합니다. 그리고 나는 여기에서 예제를 따라하더라도 그것을 얻이 수없는 것 :레일에서 조인 된 테이블의 조건을 지정하는 방법
Client.joins(:orders).where(:orders => {:created_at => time_range})
내 데이터베이스 스키마 : guides.rubyonrails.org의 가이드에서
http://guides.rubyonrails.org/active_record_querying.html#specifying-conditions-on-the-joined-tables
점수, 제출 및 작업 테이블, 다음과 같습니다
create_table "scores", :force => true do |t|
t.integer "value"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "scores", ["user_id"], :name => "index_scores_on_user_id"
create_table "submissions", :force => true do |t|
t.integer "user_id"
t.integer "task_id"
t.integer "score_id"
t.datetime "completed_at"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "submissions", ["score_id"], :name => "index_submissions_on_score_id"
add_index "submissions", ["task_id"], :name => "index_submissions_on_task_id"
add_index "submissions", ["user_id"], :name => "index_submissions_on_user_id"
create_table "tasks", :force => true do |t|
t.integer "episode_id"
t.integer "score"
t.string "key"
t.datetime "created_at"
t.datetime "updated_at"
end
그래서 난 내가 spesific 작업에 관계가있는 모든 "점수"를 찾을 수있는 쿼리를 수행합니다. 제출은 과제 및 점수에 속합니다.
내 쿼리는 이제 다음과 같습니다
Score.joins(:submission).where(:submission => {:task_id => 1})
이 다음 구문 생성
SELECT "scores".* FROM "scores" INNER JOIN "submissions" ON "submissions"."score_id" = "scores"."id" WHERE "submission"."task_id" = 1
다음과 같은 오류 발생 :
SQLite3::SQLException: no such column: submission.task_id
을하지만 열 submission.task_id
가 , db 스키마에서 볼 수 있습니다. 그리고 난 성공적으로이 작업을 수행 할 수 있습니다
SELECT "submissions".* FROM "submissions" WHERE "submissions"."task_id" = 1
실제로 레일스 스 니펫이 게시 된대로 '조인'심볼을 게시해야합니다. – Chowlett
완전히 맞습니다! 그냥 내 자신을 발견했습니다. 꿰매다. – espenhogbakk
@Chowlett, 다 대다 관계를 가지고 있기 때문에, 나는 일대일, has_one, has_many의 instad 만 갖는다. 그래서 나는 내가 생각하는 테이블 이름을 참조하여 복수형을 포착하지 않았습니다. – espenhogbakk