简体   繁体   English

Rails 5-如何使用OR条件而不是AND获取记录

[英]Rails 5 - How to get records with OR condition instead of AND

I'm trying to get a complicated piece of data from rails. 我正在尝试从Rails获取一个复杂的数据。

What I want are all users, the tasks that are associated with them and limited to specific project. 我想要的是所有用户,与他们相关联且限于特定项目的任务。

Here's the task schema for reference: 这是供参考的任务架构:

create_table "tasks", force: :cascade do |t|
t.date "start_date"
t.date "end_date"
t.integer "hours"
t.string "priority"
t.integer "project_id"
t.integer "user_id"
t.string "name"
t.string "description"

end 结束

I accomplish parts of this with this call 我通过这次电话完成了部分工作

users = User.includes(:tasks).where('tasks.end_date Between ? AND ?', Date.today.beginning_of_week, Date.today.end_of_week).references(:tasks).where('tasks.start_date Between ? AND ?', Date.today.beginning_of_week, Date.today.end_of_week).references(:tasks).where('tasks.project_id = ?', @project.id).map { |user| user.as_json.merge({tasks: user.tasks.as_json}) }

My problem is that my query is not finding the tasks based on their dates correctly. 我的问题是我的查询未根据任务的日期正确找到任务。

I am trying to find all tasks within a week range that either have a start_date or end_date within that week. 我正在尝试查找一周范围内在该周内具有start_date或end_date的所有任务。

Is this possible within one query or do I require more advanced logic? 是否可以在一个查询中实现?还是我需要更高级的逻辑?

If you are using Rails 5 you can make user of or method 如果您使用的是Rails 5,则可以使用or方法

User.joins(:tasks).where(tasks: {end_date: Date.today.beginning_of_week..Date.today.end_of_week})
    .or(
         User.joins(:tasks).where(tasks: {start_date: Date.today.beginning_of_week..Date.today.end_of_week})
        )

For sake of brevity, I haven't included project where clause. 为了简洁起见,我没有包含项目where子句。

I haven't tested this but I think what was happening was your where grabbing all of the users with tasks that end_date happens between given times and then querying those models with every user with tasks that start_date happens between given times. 我尚未对此进行测试,但是我认为正在发生的事情是您在哪里捕获所有在end_date在给定时间之间发生任务的用户,然后向每个用户在start_date在给定时间之间发生任务的用户查询这些模型。 Giving you only users whose tasks start_date and end_date happen between the given times. 仅向您提供任务start_date和end_date在指定时间之间发生的用户。

users = User.includes(:tasks).where('((tasks.end_date BETWEEN ? AND ?) OR (tasks.start_date BETWEEN ? AND ?)) AND tasks.project_id = ?', Date.today.beginning_of_week, Date.today.end_of_week, Date.today.beginning_of_week, Date.today.end_of_week, @project.id).references(:tasks).map { |user| user.as_json.merge({tasks: user.tasks.as_json}) }

Hope it helps. 希望能帮助到你。 Cheers! 干杯!

这就是最终为我工作的东西,谢谢@vijay

 User.includes(:tasks).where(tasks: {end_date:Date.today.beginning_of_week..Date.today.end_of_we‌​ek}).or(User.include‌​s(:tasks).where(task‌​s: {start_date: Date.today.beginning_of_week..Date.today.end_of_week})).wher‌​e('tasks.project_id = ?', @project.id).map { |user| user.as_json.merge({tasks: user.tasks.as_json}) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM