Rails where query 中两个属性的字符串串联

String concatenation of two attributes in Rails where query

假设我有用户的 first_namelast_name 属性。我知道用户的全名是 "Johnny Appleseed",因为我有一个很大的用户全名哈希字符串。

当然,我可以先拆分每个字符串,然后像这样搜索: User.where(first_name: 'Johnny', last_name: "Appleseed")

但是,我想知道是否有一种方法可以在查询中基本上连接两者,基本上是这样的: User.where('first_name + last_name = ?', 'Johnny Appleseed')

或者如果我可以在 User 模型上有一个 full_name 方法,并以此进行搜索?

 def full_name
    "#{self.first_name} #{self.last_name}"
  end

User.where('full_name = ?', 'Johnny Appleseed')

可能最简单的解决方案是:

User.where(%q(concat_ws(' ', first_name, last_name) = ?), 'Johnny Appleseed')