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 6203933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:00:08+00:00 2026-05-24T05:00:08+00:00

Two fixtures: one: firstname: John lastname: Doe user_id: 1 two: firstname: Jane lastname: Doe

  • 0

Two fixtures:

one:
  firstname: John
  lastname: Doe
  user_id: 1

two:
  firstname: Jane
  lastname: Doe
  user_id: 1

In my unit test, I have the following:

 test "Searching for a contact gets the right person" do
    search = Contact.search("John")
    assert_equal 1, search.count

    search = Contact.search("Doe")
    assert_equal 2, search.count

    search = Contact.search("John Doe")
    assert_equal 1, search.count

    search = Contact.search("John ")
    assert_equal 1, search.count

    search = Contact.search("Doe John")
    assert_equal 1, search.count
  end

…and finally my model, which calls the search method looks like:

  def self.search(search)
    # Check if the search query has more than one word
    search_split = search.split(" ")
    if search_split.count > 1
      # User must be searching for a first and last name
      first_word = '%' + search_split[0] + '%'
      second_word = '%' + search_split[1] + '%'
      conditions = "
        firstname ILIKE ? OR lastname ILIKE ? 
        AND 
        firstname ILIKE ? OR lastname ILIKE ?", 
        first_word, first_word, second_word, second_word
      where(conditions)
    else
      # Just searching for a first OR last name
      # Strip any whitespace
      str = search_split[0]
      query = '%' + str + '%'
      conditions = "firstname ILIKE ? OR lastname ILIKE ?", query, query
      where(conditions)
    end
  end

All tests pass except the one that does a test for “John Doe”. It actually ends up pulling “Jane Doe” as well.

I’m thinking there is some kind of precedence issue with calling the two OR’s before the adjoining AND, but does it make sense of what I’m trying to accomplish?

I’m not at the point where I’m refactoring just yet; just trying to get to green so I can move forward. Thanks for any help 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-05-24T05:00:08+00:00Added an answer on May 24, 2026 at 5:00 am

    You need a bit of reorganization to get the logic you’re after:

    conditions = "
      (firstname ILIKE ? AND lastname ILIKE ?)
      OR
      (firstname ILIKE ? AND lastname ILIKE ?)",
      first_word, second_word, second_word, first_word
    

    That should match both “John Doe” and “Doe John” but not “Jane Doe” when you call:

    Contact.search('Jo Do')
    

    You don’t actually need the parentheses because AND has a higher precedence than OR in Boolean Algebra (and SQL’s logic is based on Boolean Algebra) but there’s no good reason not to include them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my cocos2d box2d app i have two bodies, the first has one fixture
I have the following two models: School and User, and a HABTM relationship between
I'm having problems with my fixtures in Rails. I have two models, a message
I have two databases in use in a Ruby on Rails application; one is
I have two apps. One is a very simple app that I built with
Two Windows processes have memory mapped the same shared file. If the file consists
I have edited the question as what I said before was two pieces of
How do you control the order of fixture loading when two fixtures depend on
I have two projects in eclipse: framework: A general library used in several projects.
Can I return more than one item in a select? For instance I have

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.