jeudi 28 avril 2016

Rails association - Customised list of items from existing list

I am developing a rails app where users can add tasks they wish to do to a customised list of their own. Each task can also belong to 0 or more categories. So far I've tried this:

user.rb

has_one :user_list
has_many :tasks, through: :user_list

user_list.rb

belongs_to :user
has_many   :tasks

tasks.rb

has_and_belongs_to_many :categories

[timestamp}_migration.rb

create_table :user_lists do |t|
  t.integer :user_id
  t.integer :task_id

  t.timestamps null: false
end

The issue I am having is in the console I try to run User.find(1).tasks it cannot find the column tasks.user_list_id when using the following query:

SELECT "tasks".* FROM "tasks" INNER JOIN "user_lists" ON "tasks"."user_list_id" = "user_lists"."id" WHERE "user_lists"."user_id" = ?  [["user_id", 1]]

This query should be joining the tasks id from the tasks table with the tasks id on the user_lists table. Are the associations correct and if so what can I do to change the query?

Aucun commentaire:

Enregistrer un commentaire