Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails查询中两个属性的字符串连接_Ruby On Rails_Ruby On Rails 3_Activerecord - Fatal编程技术网

Ruby on rails Rails查询中两个属性的字符串连接

Ruby on rails Rails查询中两个属性的字符串连接,ruby-on-rails,ruby-on-rails-3,activerecord,Ruby On Rails,Ruby On Rails 3,Activerecord,假设我有一个用户的attrsfirst\u name和last\u name。我知道用户的全名是“Johnny Applesed”,因为我有大量的用户全名散列作为字符串 当然,我可以先拆分每个字符串,然后像这样搜索: User.where(名字:“约翰尼”,姓氏:“苹果色”) 但是,我想知道是否有一种方法可以在一个查询中基本上把这两个连接起来,基本上是这样的: User.where('first\u name+last\u name=?','Johnny Appleseed') 或者我是否可以在

假设我有一个用户的attrs
first\u name
last\u name
。我知道用户的全名是“Johnny Applesed”,因为我有大量的用户全名散列作为字符串

当然,我可以先拆分每个字符串,然后像这样搜索:
User.where(名字:“约翰尼”,姓氏:“苹果色”)

但是,我想知道是否有一种方法可以在一个查询中基本上把这两个连接起来,基本上是这样的:
User.where('first\u name+last\u name=?','Johnny Appleseed')

或者我是否可以在用户模型上有一个全名方法,并以此进行搜索

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

User.where('full_name=?','Johnny applesed')
最简单的解决方案可能是:

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

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

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

这很有效,谢谢!就性能而言,虽然速度很慢,但它完成了任务。我不知道有什么新东西,谢谢你给我看。这很有效,谢谢!就性能而言,虽然速度很慢,但它完成了任务。我不知道有什么新东西,谢谢你给我看。