Ruby on rails 在Rails 4中使用“CONCAT”w/Arel

Ruby on rails 在Rails 4中使用“CONCAT”w/Arel,ruby-on-rails,ruby,ruby-on-rails-4,arel,Ruby On Rails,Ruby,Ruby On Rails 4,Arel,我有一个用户模型,它具有名字和姓氏属性。使用Arel,我想使用CONCAT执行全名搜索。我读过一篇文章,文章中指出这是可能的,但我不能完全正确地理解语法。到目前为止我有 class User < ActiveRecord::Base def self.search(query) concat = Arel::Nodes::NamedFunction.new 'concat', [arel_table[:first_name], arel_table[:last_name]]

我有一个
用户
模型,它具有
名字
姓氏
属性。使用Arel,我想使用
CONCAT
执行全名搜索。我读过一篇文章,文章中指出这是可能的,但我不能完全正确地理解语法。到目前为止我有

class User < ActiveRecord::Base
  def self.search(query)
    concat = Arel::Nodes::NamedFunction.new 'concat', [arel_table[:first_name], arel_table[:last_name]]
    where ...?
  end
end
class用户
如果您希望进行相同的搜索

 where(concat.eq("john smith"))

 SELECT "users".* FROM "users" WHERE CONCAT("users"."first_name", "users"."last_name") = 'john smith'
如果你想要一个类似的搜索

where(concat.matches("%john smith%"))

 SELECT "users".* FROM "users" WHERE (CONCAT("users"."first_name", "users"."last_name")) ILIKE '%admin%'
中给出的其他方法也可以使用

您可能希望在您的concat中有一个空间

concat = Arel::Nodes::NamedFunction.new(
 'concat',
  [arel_table[:first_name], 
  ' ', 
  arel_table[:last_name]]
)

在最新的Arel中,需要使用
Arel::Nodes.build_quoted(“”)
而不仅仅是字符串(“”)。因此,现在的答案是:

SEPARATOR = Arel::Nodes.build_quoted(' ')

Arel::Nodes::NamedFunction.new(
  'concat',
  [arel_table[:first_name], SEPARATOR, arel_table[:last_name]]
)

有没有一种DB不可知的方法来实现这一点?现在,我必须检查我的DB适配器(臭味)在SQLite:“first|name |”“last|u name”和Postgres:SQLite:“CONCAT(first|name,”,last|name)“2015年底Arel的CONCAT节点。
Arel::Nodes.build|u quoted(“”)
在Arel>=6中定义,它对我不起作用(Rails 4.0)。在这种情况下,只需将其替换为
'