mySQL to PostgreSQL concat first name and last name for name search (Ruby on Rails) -


hey i'm trying figure out how convert statement works in mysql postgresql , curious if knows solution.

here statement works in mysql:

def self.by_name(keywords)     if keywords         find(:all, :conditions => ["concat(first_name," ",last_name) like?", "%#{keywords}%"])      end   end 

here statement found on this site had similar problem, doesn't work me, in, if search contact.by_name("bobby"), there no results.

def self.by_name(keywords)     if keywords         find(:all, :conditions => ["textcat(textcat(first_name,text ' '),last_name) like?", "%#{keywords}%"])     end   end 

the idea search "bobby", "fishcer", or "bobby fischer" , match either first name, last name, or both first , last name. thanks!

you can use concatenation operator (||) paste together:

:conditions => [   "coalesce(first_name, '') || ' ' || coalesce(last_name, '') ilike '%' || ? || '%'",   keywords ] 

you'll notice switched string interpolation, "%#{keywords}%", string concatenation in sql, '%' || ? || '%', build search pattern; doing avoids injection , quoting problems. i've switched ilike since want case insensitive search.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -