Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9162137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:08:27+00:00 2026-06-17T14:08:27+00:00

I am trying to use the following sql statement in a Rails application with

  • 0

I am trying to use the following sql statement in a Rails application with Rails query helper methods. The result of this sql in the original (non-Rails) application is to do a fuzzy search on someone’s name with any letter. For example, if you enter an “a” in the search box, it will return everyone name that has the letter “a” in it somewhere. “Marissa Jones” and “Andrea Barfs” would both be matches.

      "select e.id, e.firstName, e.lastName, e.title " .
      "from employee e left join employee r on r.managerId = e.id " .
      "WHERE UPPER(CONCAT(e.firstName, ' ', e.lastName)) LIKE :name " .
      "group by e.id order by e.lastName, e.firstName";

I tried to translate that sql statement using some of Rails active record query interface helper methods. (I didn’t attempt to include the ‘join’ from the above sql because it didn’t seem necessary for the fuzzy search or for the purposes of my app). There is one user supplied parameter :query

employees_controller.rb

respond_to :json
 ...
def getEmployeesByName

    query = Employee.select("id, firstname, lastname, title")
    q = "%#{params[:query]}%"
    query.where("UPPER(CONCAT(firstName, ' ', lastName)) LIKE ?", q).to_sql
    query.group("id")
    query.order("lastname", "firstname")

    respond_with query.all 

end 

When I try my code, it’s returning all of the 5 employees in the database, even if they don’t have the letter “a” in their name. This is the sql shown in the server, when, for example, I enter an “a” in the search box.

  Processing by EmployeesController#getEmployeesByName as JSON
  Parameters: {"query"=>"a"}
  Employee Load (0.1ms)  SELECT id, firstname, lastname, title FROM "employees" 

So it seems the last four lines of my action are being ignored

    query.where("UPPER(CONCAT(firstName, ' ', lastName)) LIKE ?", q).to_sql
    query.group("id")
    query.order("lastname", "firstname")
    respond_with query.all 

Can anyone explain what I’m doing wrong. Thank you in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:08:28+00:00Added an answer on June 17, 2026 at 2:08 pm

    query is being assigned an instance of Employee by the first line. You are then calling where, group, and order on the class Employee via the instance, but ignoring the result in every case. Finally, you are calling Employee.all via the instance, and inspecting the result, which correctly includes all records.
    Try something like:

    respond_with Employee.where("UPPER(CONCAT(firstName, ' ', lastName)) LIKE ?", q).group("id").order("lastname", "firstname")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use the following code to run a PL/SQL statement on my
I am trying to query 2 tables using the following sql statement in an
I'm trying to use a CASE statement in a SQL query I have and
I am trying to use a SQL Select statement for a query in Java.
I'm trying to get my MVC3 application to use MySql database instead of Sql
I'm trying to execute the following SQL statement (built and tested in SQL Server
I am trying the following SQL statement: create table sample ( id int not
I am trying to convert following SQL query to linq; select Or.Amount, Usr.Name, Usr.Email
I have the following SQL query that I am trying to run inside Microsoft
I've been trying to query an email address using the following statement, however after

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.