我有一个meetings表,其中有一个请求者(用户)具有属性birthyear。
在终端中我可以做会议。查找(1)。请求者。生日
但在会议控制器中,它显示no such column: requestor.birthyear
我试着在meetings参数中添加类似于users_attributes: [:birthyear]
的内容,但没有任何区别。
我错过了什么?
以下是完整的文件:
会议管理员
def community
if current_user.birthyear != nil
@meeting_requests = Meeting.where('meeting_time >= ? AND requestee_id IS ? AND status = ? AND (requestor.birthyear <= ? AND requestor.birthyear >= ?)', Date.today, nil, "Active", current_user.birthyear + 10, current_user.birthyear - 10).order('meeting_time asc')
end
end
会议模式
class Meeting < ApplicationRecord
belongs_to :requestor, class_name: "User"
belongs_to :requestee, class_name: "User"
end
错误:
SQLite3::SQLException: no such column: requestor.birthyear: SELECT "meetings".* FROM "meetings" WHERE (meeting_time >= '2017-03-06' AND requestee_id IS NULL AND status = 'Active' AND (requestor.birthyear <= 1944 AND requestor.birthyear >= 1924)) ORDER BY meeting_time asc
如果你还需要什么,请告诉我-提前谢谢!
更新
尝试在查询中加入表,但对这一点了解不多。我正在努力:
@meeting_requests = Meeting.where('meeting_time >= ? AND requestee_id IS ? AND status = ? ', Date.today, nil, "Active").order('meeting_time asc').joins("INNER JOIN users on requestor.birthyear >= current_user.birthyear + 10")
但得到:
ActionView::Template::Error (SQLite3::SQLException: ambiguous column name: status: SELECT "meetings".* FROM "meetings" INNER JOIN users on requestor.birthyear >= current_user.birthyear + 10 WHERE (meeting_time >= '2017-03-07' AND requestee_id IS NULL AND status = 'Active' ) ORDER BY meeting_time asc):
*****更新*****
我发布了另一个问题来获取语法。如果它能帮助任何人:
Rails: Two 'where' queries - each works individually, but not together
最佳答案
requestor
和requestee
是Rails关联。这意味着rails了解如何从会议中获取requestor
。
但是-您的数据库不知道requestor
是什么。因此,不能像在这里那样将它放入sql查询的字符串部分:
AND (requestor.birthyear <= ? AND requestor.birthyear >= ?)'
相反,您需要使用连接将它们添加到查询中,然后找出rails如何命名它们,以便从中访问列。
关于ruby-on-rails - 列在那里,但我收到:SQLite3::SQLException:没有这样的列:requestor.birthyear,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42639250/