gpt4 book ai didi

ruby-on-rails - 尝试按名字和姓氏搜索

转载 作者:数据小太阳 更新时间:2023-10-29 07:55:50 25 4
gpt4 key购买 nike

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

用户 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 %>

最佳答案

这个:

: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 函数。据我所知,SQLite 支持很多 MySQL 主义,但您在使用它们时需要注意,并且应尽可能遵守标准。
  2. 再次强调,引用是特定于数据库的。标准 SQL 对字符串文字使用单引号,对标识符(例如表名和列名)使用双引号。 MySQL 对标识符使用反引号,SQLite(AFAIK)允许您对标识符使用双引号或反引号,对字符串使用单引号或双引号。再次,尽可能坚持标准,养成良好的习惯。

关于ruby-on-rails - 尝试按名字和姓氏搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25945227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com