Any suggestion why im getting this message in my Simple Search ? ** i want to search in multiple rows in my db. thanks SyntaxError in PostsController#index /Users/webstic/Sites/weblog/app/models/post.rb:7: syntax error, unexpected tCONSTANT, expecting '']'' ...onditions => [''body LIKE?'' AND ''title LIKE?'', "%#{search}%"]... ^ /Users/webstic/Sites/weblog/app/models/post.rb:7: syntax error, unexpected '']'', expecting kEND ... ''title LIKE?'', "%#{search}%"], :order => ''created_at DESC'' ^ code @ models/posts.rb def self.search(search, page) paginate :per_page => 3, :page => page, :conditions => [''body LIKE?'' AND ''title LIKE?'', "%#{search}%"], :order => ''created_at DESC'' end end -- Posted via http://www.ruby-forum.com/.
:conditions => [''body LIKE?'' AND ''title LIKE?'', "%#{search}%"] Try fixing the condition clause to: :conditions => [''body LIKE ? AND title LIKE ?'', "%#{search}%", "%# {search}%" ] You are passing an array containing the SQL "WHERE" clause in a template. Each ''?'' is a placeholder for the variables following the initial string containing the condition. For every ''?'' in the clause there has to be a corresponding variable to be inserted. Your original had two placeholders and only one variable. Placeholders show where the corresponding variables will be inserted. In your code the variable would have been inserted as a run-on to the "LIKE" which SQL would have complained about because it would have been a syntax error. The ''AND'' is a boolean in the SQL statement and has to be part of it. Ruby was getting confused by your original statement because the "AND" was outside the single-quotes and thought you were ANDing two strings. On Aug 15, 6:22 pm, Philip Gavrilos <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Any suggestion why im getting this message in my Simple Search ? > > ** i want to search in multiple rows in my db. > > thanks > > SyntaxError in PostsController#index > > /Users/webstic/Sites/weblog/app/models/post.rb:7: syntax error, > unexpected tCONSTANT, expecting '']'' > ...onditions => [''body LIKE?'' AND ''title LIKE?'', "%#{search}%"]... > ^ > /Users/webstic/Sites/weblog/app/models/post.rb:7: syntax error, > unexpected '']'', expecting kEND > ... ''title LIKE?'', "%#{search}%"], :order => ''created_at DESC'' > ^ > > code @ models/posts.rb > > def self.search(search, page) > paginate :per_page => 3, :page => page, > :conditions => [''body LIKE?'' AND ''title LIKE?'', > "%#{search}%"], :order => ''created_at DESC'' > end > end > -- > Posted viahttp://www.ruby-forum.com/.
Philip Gavrilos
2009-Aug-16 21:32 UTC
Re: querying multiple rows from db in my simple search
g_f wrote:> :conditions => [''body LIKE?'' AND ''title LIKE?'', "%#{search}%"] > > Try fixing the condition clause to: > > :conditions => [''body LIKE ? AND title LIKE ?'', "%#{search}%", "%# > {search}%" ] > > You are passing an array containing the SQL "WHERE" clause in a > template. Each ''?'' is a placeholder for the variables following the > initial string containing the condition. For every ''?'' in the clause > there has to be a corresponding variable to be inserted. Your original > had two placeholders and only one variable. Placeholders show where > the corresponding variables will be inserted. In your code the > variable would have been inserted as a run-on to the "LIKE" which SQL > would have complained about because it would have been a syntax error. > > The ''AND'' is a boolean in the SQL statement and has to be part of it. > Ruby was getting confused by your original statement because the "AND" > was outside the single-quotes and thought you were ANDing two strings. > > On Aug 15, 6:22�pm, Philip Gavrilos <rails-mailing-l...@andreas-s.net>thats work for me! thanks -philip.g -- Posted via http://www.ruby-forum.com/.