ruby-on-rails - 如何将 SQL 语法转换为 Mongo?
问题描述
我使用 Ruby on Rails 和 Mongoid
如何转换此语法以使其与 Mongo 一起使用?
def index
@conversations = Conversation.where("sender_id = ? OR receiver_id = ?", current_user.id, current_user.id)
end
和
def index
@conversation.messages.where("user_id != ? AND read = ?", current_user.id, false).update_all(read: true)
end
解决方案
找到了解决方案:
@conversations = Conversation.where("sender_id = ? OR receiver_id = ?", current_user.id, current_user.id)
改成
@conversations = Conversation.any_of({sender_id: current_user.id}, {receiver_id: current_user.id})
第二行必须完全重写并且不使用。所以我不知道它是否有效
@conversation.messages.where("user_id != ? AND read = ?", current_user.id, false).update_all(read: true)
可能行不通
@conversation.messages.not.where(user_id: current_user.id).where(read: false).update_all(read: true)
推荐阅读
- mysql - Add a constraint so one date is not less than or equal to the other date in the same table in MySQL
- firebase - 如何避免读取我对 Firestore 不感兴趣的文档?
- google-apps-script - Trigger script when a specific cell value in another sheet chan
- rest - REST: Get multiple resources - what is correct REST approach
- excel - How can I add previous column values to to get new value in Excel?
- azure - MSAL error "parsing_wstrust_response_failed" when trying to use AcquireTokenByIntegratedWindowsAuth
- django - Django Rest Framework,在模型级别定义的“lookup_field”值
- c++ - 在 C++ DLL 中触发事件并在客户端(使用 DLL 的 C++ 或 C# 应用程序)中处理它的正确方法是什么?
- c - 当我期望字母字符时,程序会产生数字
- html - HTML5 文件上传延迟