【问题标题】:Trying to search by first_name and last_name尝试按 first_name 和 last_name 搜索
【发布时间】:2014-09-20 04:27:20
【问题描述】:

我正在尝试在我的 rails 应用程序中同时按名字和姓氏搜索用户,目前我尝试的每种方法都得到了不同的结果。有没有办法重写这些方法来获得我想要的结果?

user_controller.rb

方法#1

def self.search(query)
  where("first_name LIKE ? OR last_name LIKE ?", "%#{query}%", "%#{query}%") 
end

这适用于名字或姓氏,但不能同时适用。

方法#2

def self.search(keywords)
  if keywords
    where(:all, :conditions => ["concat(first_name," ",last_name) like?", "%#{keywords}%"])
  end
end

这不会返回任何结果

方法#3

def self.search(search)
  if search
    select('(first_name || " " || last_name) as \'ful_name\', *')
    where ['first_name LIKE :s OR last_name LIKE :s OR ful_name LIKE :s', :s => "%#{search}"]
  else
  scoped
 end
end

这会返回错误

SQLite3::SQLException: no such column: ful_name: SELECT "users".* FROM "users" WHERE (first_name LIKE '%Spider Man' OR last_name LIKE '%Spider Man' OR ful_name LIKE '%Spider Man') ORDER BY created_at DESC

app/views/users/index.html.erb:5:in `_app_views_users_index_html_erb__848623016_40254132'

index.html.erb

<% provide(:title, 'Search') %>
<h1>Search</h1>

<ul class="span4 users">
  <%= render @users %>
</ul>

_user.html.erb

<li>
  <%= image_tag user.avatar(:medium) %>
  <h4><%= link_to user.full_name, feed_user_path(user), :class => "follow-color" %></h4>
  <% if current_user.admin? && !current_user?(user) %>
    | <%= link_to "delete", user, method: :delete,
                                  data: { confirm: "You sure?" } %>
  <% end %>
</li>

_header.html.erb

<%= form_tag users_path, method: "get", class: "search-bar" do %>
  <%= text_field_tag :search, params[:search], placeholder: "Search" %>
<% end %>

【问题讨论】:

  • 第一个 varitan 看起来是正确的,但是 This works for either first or last name but not both. 是什么意思?
  • 它将根据我在数据库中的名字或姓氏字符串进行搜索,但不会搜索 first_name + last_name

标签: ruby-on-rails ruby ruby-on-rails-4 rails-activerecord


【解决方案1】:

这个:

:conditions => ["concat(first_name," ",last_name) like?", "%#{keywords}%"]

因为你有一个(隐蔽的)报价问题,所以不起作用。在 Ruby 中,这是:

"a" "b"

等同于:

"ab"

所以你的:conditions 真的是这样的:

:conditions => ["concat(first_name,,last_name) like?", "%#{keywords}%"]

你的意思是说:

:conditions => ["concat(first_name, ' ', last_name) like ?", "%#{keywords}%"]

SQL 中的字符串文字使用单引号,而不是双引号。此外,如果您使用的数据库声称支持标准 SQL,则应使用 || 运算符进行字符串连接:

:conditions => ["first_name || ' ' || last_name like ?", "%#{keywords}%"]

第三个不起作用,因为在 SELECT 子句中定义的别名在 WHERE 子句中通常不可用,因此出现“未知列”错误。您还丢弃了select 调用的结果,所以我认为您在这里也缺少.

select('(first_name || " " || last_name) as \'ful_name\', *')
where ['first_name LIKE :s OR last_name LIKE :s OR ful_name LIKE :s', :s => "%#{search}"]

还有一个潜在的引用问题:字符串文字在 SQL 中使用单引号,双引号用于标识符。你只想说:

where("first_name like :s or last_name like :s or first_name || ' ' || last_name like :s", :s => "%#{search}")

或者只是:

where("first_name || ' ' || last_name like :s", :s => "%#{search}")

几个注意事项:

  1. 字符串连接运算符是特定于数据库的。标准 SQL 使用 ||,但根据配置,MySQL 想要使用 concat 函数。 AFAIK,SQLite 支持很多 MySQL-isms,但您在使用它们时需要注意,并且应该尽可能地遵守标准。
  2. 同样,引用是特定于数据库的。标准 SQL 对字符串文字使用单引号,对标识符(例如表名和列名)使用双引号。 MySQL 对标识符使用反引号,SQLite (AFAIK) 允许您对标识符使用双引号或反引号,对字符串使用单引号或双引号。再次强调,尽可能坚持标准以养成良好习惯。

【讨论】:

  • @RichPeck:谢谢。简单的“do X”答案让我感觉不对。
【解决方案2】:

我也想做同样的事,我跟着这个好教程:https://www.youtube.com/watch?v=s88Uc0InOAM

然后我遇到了同样的问题,我想同时搜索姓名和姓氏。 这就是我所做的,它正在工作:

    @indiens = Indien.where("name LIKE ?", "%" + params[:q] + "%" ).or(Indien.where("surname LIKE ?", "%" + params[:q] + "%" ))

也许可以链接更多,但我没有测试。

我知道已经 5 年了,也许会对其他人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多