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

2023-09-09 22:06:01 作者:Garbage

我在写一个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

 
精彩推荐
图片推荐