当 :has_and_belongst_to_many 时,在 Rails 中嵌套 sql 查询

发布于 2024-10-11 01:56:12 字数 888 浏览 3 评论 0原文

在我的应用程序中,我执行用户尚未完成的下一个任务。我有三个模型,一本有许多任务,然后我有一个拥有并属于许多任务的用户。表task_users 表包含所有已完成的任务,因此我需要编写一个复杂的查询来查找下一个要执行的任务。

我在纯 SQL 中提出了两个可行的解决方案,但我无法将它们转换为 Rails,这就是我需要的帮助

SELECT * FROM `tasks`
WHERE `tasks`.`book_id` = @book_id
AND `tasks`.`id` NOT IN (
    SELECT `tasks_users`.`task_id`
    FROM `tasks_users`
    WHERE `tasks_users`.`user_id` = @user_id)
ORDER BY `task`.`date` ASC
LIMIT 1;

,同样没有嵌套选择

SELECT *
FROM tasks
LEFT JOIN tasks_users
    ON tasks_users.tasks_id = task.id
    AND tasks_users.user_id = @user_id
WHERE tasks_users.task_id IS NULL
AND tasks.book_id = @book_id
LIMIT 1;

这是我使用 MetaWhere 插件在 Rails 中所做的,

book.tasks.joins(:users.outer).where(:users => {:id => nil})

但我不知道如何也让当前用户也到达那里,

感谢您的帮助!

In my application I the next task that has not already been done by a user. I have Three models, A Book that has many Tasks and then I have a User that has has and belongs to many tasks. The table tasks_users table contains all completed tasks so I need to write a complex query to find the next task to perform.

I have came up with two solutions in pure SQL that works, but I cant translate them to rails, thats what I need help with

SELECT * FROM `tasks`
WHERE `tasks`.`book_id` = @book_id
AND `tasks`.`id` NOT IN (
    SELECT `tasks_users`.`task_id`
    FROM `tasks_users`
    WHERE `tasks_users`.`user_id` = @user_id)
ORDER BY `task`.`date` ASC
LIMIT 1;

and equally without nested select

SELECT *
FROM tasks
LEFT JOIN tasks_users
    ON tasks_users.tasks_id = task.id
    AND tasks_users.user_id = @user_id
WHERE tasks_users.task_id IS NULL
AND tasks.book_id = @book_id
LIMIT 1;

This is what I Have done in rails with the MetaWhere plugin

book.tasks.joins(:users.outer).where(:users => {:id => nil})

but I cant figure out how to get the current user there too,

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬色的秋千 2024-10-18 01:56:13

我认为这将使用 LEFT JOIN 重复第二种形式:

class Task < ActiveRecord::Base
  scope :next_task, lambda { |book,user| book.tasks.\
    joins("LEFT JOIN task_users ON task_users.task_id=tasks.id AND task_users.user_id=#{user.id}").\
    where(:tasks=>{:task_users=>{:task_id=>nil}}).\
    order("date DESC").limit(1) }
end

请注意,它使用表名 task_user 而不是 tasks_users,这对于连接模型来说更为典型。另外,它需要通过以下方式调用:

Task.next_task(@book_id,@user_id)

I think this will duplicate the second form with the LEFT JOIN:

class Task < ActiveRecord::Base
  scope :next_task, lambda { |book,user| book.tasks.\
    joins("LEFT JOIN task_users ON task_users.task_id=tasks.id AND task_users.user_id=#{user.id}").\
    where(:tasks=>{:task_users=>{:task_id=>nil}}).\
    order("date DESC").limit(1) }
end

Note that instead of tasks_users this uses the table name task_user, which is more typical for a join model. Also, it needs to be called with:

Task.next_task(@book_id,@user_id)
有木有妳兜一样 2024-10-18 01:56:13
book.tasks.where("tasks.id not in (select task_id from tasks_users where user_id=?)", @user_id).first

这将为您提供第一个任务,该任务在当前用户的tasks_users 中还没有条目。

book.tasks.where("tasks.id not in (select task_id from tasks_users where user_id=?)", @user_id).first

That would give you the first task that doesn't already have an entry in tasks_users for the current user.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文