Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to specify conditions on joined tables in rails
    primarykey
    data
    text
    <p>I am trying to do a query in in Rails with ActiveRecord that specifies some condition on a joined table. And i can't seem to get it to work, even though i follow the examples from here:</p> <p><a href="http://guides.rubyonrails.org/active_record_querying.html#specifying-conditions-on-the-joined-tables" rel="nofollow noreferrer">http://guides.rubyonrails.org/active_record_querying.html#specifying-conditions-on-the-joined-tables</a></p> <p>From the <a href="http://guides.rubyonrails.org" rel="nofollow noreferrer">guides</a>:</p> <blockquote> <p><code>Client.joins(:orders).where(:orders =&gt; {:created_at =&gt; time_range})</code></p> </blockquote> <p>My database schema looks like this, with tables <code>scores</code>, <code>submissions</code> and <code>tasks</code>:</p> <pre><code> create_table "scores", :force =&gt; 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 =&gt; "index_scores_on_user_id" create_table "submissions", :force =&gt; 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 =&gt; "index_submissions_on_score_id" add_index "submissions", ["task_id"], :name =&gt; "index_submissions_on_task_id" add_index "submissions", ["user_id"], :name =&gt; "index_submissions_on_user_id" create_table "tasks", :force =&gt; true do |t| t.integer "episode_id" t.integer "score" t.string "key" t.datetime "created_at" t.datetime "updated_at" end </code></pre> <p>So i want to do a query where I can find all "scores" that have a relation to a spesific task. Submission belongs to tasks and scores.</p> <p>My query now looks like this:</p> <pre><code>Score.joins(:submission).where(:submission =&gt; {:task_id =&gt; 1}) </code></pre> <p>This generates the following syntax:</p> <pre><code>SELECT "scores".* FROM "scores" INNER JOIN "submissions" ON "submissions"."score_id" = "scores"."id" WHERE "submission"."task_id" = 1 </code></pre> <p>Which generates the following error:</p> <pre><code>SQLite3::SQLException: no such column: submission.task_id </code></pre> <p>But there is a column <code>submission.task_id</code>, which you can see in the db schema. And i can do this successfully:</p> <pre><code>SELECT "submissions".* FROM "submissions" WHERE "submissions"."task_id" = 1 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload