scope :between, -> (sender_id,recipient_id) do
where(“(conversations.sender_id = ? AND conversations.recipient_id = ? ) OR (conversations.sender_id = ? AND conversations.recipient_id =?)”, sender_id, recipient_id, recipient_id, sender_id)
end
我不断收到错误消息:
warning: invalid character syntax; use ?\s
我该如何解决?
答案 0 :(得分:1)
您的引号不是标准的引号,而是一些奇怪的utf8字符,而应使用“或'。错误消息会引起误解。某些编辑器正在使用这种格式,不适用于rails。
答案 1 :(得分:1)
您正在使用斜双引号(“
和”
),它不是Ruby中文字的有效字符串定界符。通常,如果要将文本复制到编辑器中而不是用于像MS Word这样的编程,通常会发生这种情况。有效分隔符为'
和"
。
通过使用named代替顺序的占位符,还可以大大提高可读性。
scope :between, -> (sender_id,recipient_id) do
where(
"(conversations.sender_id = :s AND conversations.recipient_id = :r ) OR (conversations.sender_id = :r AND conversations.recipient_id = :s)",
s: sender_id, r: recipient_id
)
end