我目前正在为我的rails应用程序编写一个搜索方法,目前它工作正常.我的game.rb中有以下内容:
def self.search(search)
if search
find(:all, :conditions => ['game_name LIKE ? OR genre LIKE ? OR console LIKE ?', "%#{search}%", "#{search}", "#{search}"])
else
find(:all)
end
end
Run Code Online (Sandbox Code Playgroud)
现在搜索很好,但我的问题是如果game_name中有一个记录中有'playstation'字样的记录,它将在那里完成搜索.它只返回该记录,而不是所有存储在控制台中的"playstation"的游戏.现在我明白这是因为我的条件中有'或',但我不知道另一种选择.'AND'要求所有条件匹配或根本不返回.什么是我可以使用AND和OR的替代方案?非常感谢帮助.
如果有一个解决方案有单独的搜索框和条目,那就没问题,我不一定要求搜索根据一个搜索表单找到所有搜索框.
我目前将此作为搜索:
<%= form_tag users_path, :controller => 'users', :action => 'townsearch', :method => 'get' do %>
<%= select_tag :county, params[:county] %>
<%= submit_tag 'search'%>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我的用户模型中有以下列表:
COUNTY_OPTIONS = [ "Avon", "Bedfordshire", "Berkshire", "Borders", "Buckinghamshire", "Cambridgeshire","Central",
"Cheshire", "Cleveland", "Clwyd", "Cornwall", "County Antrim", "County Armagh", "County Down",
"County Fermanagh", "County Londonderry", "County Tyrone", "Cumbria", "Derbyshire", "Devon",
"Dorset", "Dumfries and Galloway", "Durham", "Dyfed", "East Sussex", "Essex", "Fife", "Gloucestershire",
"Grampian", "Greater Manchester", "Gwent", "Gwynedd County", "Hampshire", "Herefordshire", "Hertfordshire",
"Highlands and Islands", "Humberside", "Isle …
Run Code Online (Sandbox Code Playgroud) 在我的rails应用程序中,我目前有一大堆用户,每个用户都有一个注册用户ID.
如果我进入我的用户索引并单击用户显示页面,我会得到以下示例标题.
本地主机:3000 /用户/ 3
现在我不喜欢这个,因为它很容易让人们跳过标题中的用户.
我将如何进行以下操作,以便它显示user.username字段,例如
本地主机:3000 /用户/ adamwest