【问题标题】:Rails: Two 'where' queries - each works individually, but not togetherRails:两个“where”查询 - 每个单独工作,但不能一起工作
【发布时间】:2017-07-28 00:06:50
【问题描述】:

我想写这样的东西:

@meeting_requests = Meeting.where('meeting_time >= ? AND requestee_id IS ? 
                                   AND status = ?', Date.today, nil, "Active")
                           .joins(:requestor)
                           .where('birthyear >= ? AND birthyear <= ?',
                                  current_user.birthyear - 10, 
                                  current_user.birthyear + 10 )

这行得通:

@meeting_requests = Meeting.where('meeting_time >= ? AND requestee_id IS ? 
                                   AND status = ?', Date.today, nil, "Active")

这很有效:

@meeting_requests = Meeting.joins(:requestor)
                           .where('birthyear >= ? AND birthyear <= ?',
                                   current_user.birthyear - 10,
                                   current_user.birthyear + 10 )

类似这样的工作:

Meeting.joins(:requestor).where('birthyear > ?', 1900).where(status: "Active")

但是我需要对 meeting_time 进行大于查询,所以我需要将其写成我认为的字符串?

但是两个 sql 查询一起产生错误:ambiguous column name: status: SELECT

我觉得我很接近......我在这里错过了什么?

【问题讨论】:

    标签: sql ruby-on-rails sqlite activerecord


    【解决方案1】:

    这是在不清楚列来自哪个表时出现的消息。这应该有效:

    ...rest_of_statement.where('meetings.status' => 'Active')
    

    【讨论】:

    • 哇...这工作。为什么它会知道 meeting_time 和 requestee_id 都与会议相关联,但不知道状态?我看不出这些属性之间的区别。非常感谢!!
    • 也许requestors 表中有一个status 字段?这应该是一个原因,因为通过加入您正在触发一个引用两个表的选择语句。因此,通过请求 status 不清楚您是指 requestors.status 还是 meetings.status
    • 哦,有趣!是 - 用户有一个状态字段,请求者是用户的外键。谜团解开了……非常感谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    相关资源
    最近更新 更多