【发布时间】:2015-06-22 22:49:50
【问题描述】:
假设我有一个用户的 attrs first_name 和 last_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')
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 activerecord