How to do a Sequel query with 'like'?

孤人 提交于 2019-12-10 19:24:48

问题


I'm trying to implement a form to search through the title of my posts.

This is the controller code:

post '/search'  do
@results = Post.all(:Title.like => "%#{params[:query]}%")
erb :layout 
end

This is the layout.erb code:

<form action="/search" method="post">
 <input type="text" name="query"/><br />   
 <input type="submit" />
</form>
<% if @results %>
 <table>
  <%@results.each do |r|%>
  <tr valign="top">
  <td><%=r.Title%></td>
  </tr>
  <%end%>
 </table>
<% end %>

I get an error saying 'undefined method `like' for: Title: Symbol'.


回答1:


Try

@results = DB[:posts].where(Sequel.like(:Title, "%#{params[:query]}%"))




回答2:


@results = DB[:posts].where{title.like("%#{params[:query]}%")}

ref.: https://github.com/jeremyevans/sequel/issues/1103



来源:https://stackoverflow.com/questions/20474810/how-to-do-a-sequel-query-with-like

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!