Rails的使用SQL IN和SQL或运营商3的ActiveRecord查询

编程入门 行业动态 更新时间:2024-10-26 23:35:45
本文介绍了Rails的使用SQL IN和SQL或运营商3的ActiveRecord查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在写一个Rails 3的ActiveRecord查询中使用的里语法,同时使用中的SQL和SQL或经营无法弄清楚如何使用他们两个在一起。

I'm writing a Rails 3 ActiveRecord query using the "where" syntax, that uses both the SQL IN and the SQL OR operator and can't figure out how to use both of them together.

这code ++工程(在我的用户模型):

This code works (in my User model):

Question.where(:user_id => self.friends.ids) #note: self.friends.ids returns an array of integers

但这code

but this code

Question.where(:user_id => self.friends.ids OR :target => self.friends.usernames)

返回该错误

syntax error, unexpected tCONSTANT, expecting ')' ...user_id => self.friends.ids OR :target => self.friends.usern...

不知道如何写在Rails或正是原始的SQL查询应该是什么?

Any idea how to write this in Rails, or just what the raw SQL query should be?

推荐答案

原始的SQL

SELECT * FROM table WHERE user_id in (LIST OF friend.ids) OR target in (LIST OF friends.usernames)

与每个列表逗号分隔。我不知道Rails的ActiveRecord的东西那么好。对于和你只想把那两个条件之间用逗号,但IDK的约或

with each list comma separate. I don't know the Rails ActiveRecord stuff that well. For AND you would just put a comma between those two conditions, but idk about OR

更多推荐

Rails的使用SQL IN和SQL或运营商3的ActiveRecord查询

本文发布于:2023-10-22 08:11:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1516952.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:运营商   Rails   SQL   ActiveRecord

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!