How to do a Sequel query with 'like'?
Asked Answered
P

2

5

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'.

Pah answered 9/12, 2013 at 15:53 Comment(1)
Running into the same issue, which should work according to doc (sequel.jeremyevans.net/rdoc/classes/Sequel/Model/…) - or i mix up dataset and model.Buffalo
D
5

Try

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

Directed answered 9/12, 2013 at 16:44 Comment(2)
Would this return Sequel::Models? I would need that in my Padrino app, but are currently stuck on finding out where DB is hidden from me ;)Buffalo
I am still utterly confused with Dataset/Model, but this helped: Seminar.grep(:description, '%emei%').allBuffalo
U
2

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

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

Ulna answered 10/6, 2017 at 22:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.