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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:34:03+00:00 2026-05-25T23:34:03+00:00

After a very long search I’m finally asking this question: How do I do

  • 0

After a very long search I’m finally asking this question:
How do I do OR condition queries with ActiveRecord::Relation in Rails 3.0? Basically I believe it has to do something with scoping and stuff….

Here is what I have:

class Practice < ActiveRecord::Base
  attr_accessible :name

  has_and_belongs_to_many :doctors
  has_and_belongs_to_many :services
end

class Doctor < ActiveRecord::Base
  attr_accessible :first_name, :last_name

  has_and_belongs_to_many :practices
  has_and_belongs_to_many :services
end

class Service < ActiveRecord::Base
  attr_accessible :name, :about

  has_and_belongs_to_many :practices
  has_and_belongs_to_many :doctors
end

What I want to achieve is a form where I can do a search queries and get back ActiveRecord::Relation object(so I can use it with will_paginate and so on) with list of Practices. The query should find any Practice where @practice.name matches query_string OR where @practice.doctors.first_names matches query_string OR where @practice.doctors.last_names matches query_string OR @practice.services.names matches query_string. In other words – lets say query_string matches name of a Service the I would like to get back list of Practices related to this Service. Or if first_name or last_name of a Doctor matches query_string then I get Practices associated to this Doctor/Doctors. And of course good old match by Practice.name too 😀

I realize that my has_and_belongs_to_many associations aren’t good here, it’s just for demonstration purpose but I would be happy to hear if someone can help with migrations and associations for my case.

Thanks.

P.S. as I said I use Rails 3.0 and I believe it has to do something about scopes(but I can be wrong of course). Also I’ve tried MetaWhere but couldn’t make it work…. 🙁

  • 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-25T23:34:04+00:00Added an answer on May 25, 2026 at 11:34 pm

    Let’s see how this turns out:

    class Practice < ActiveRecord::Base
      class < self
        def by_any_name(param)
          joins(:doctors, :services).
          where(<<-SQL, :name => param)
                 doctors.first_name = :name OR
                 doctors.last_name  = :name OR
                 practices.name     = :name OR
                 services.name      = :name
          SQL
        end
      end
    end
    

    Scopes are so last-year.

    And some left joins fer ya (needs testing!):

    joins('LEFT JOIN
             (doctors_practices INNER JOIN doctors
                ON doctors.id = doctors_practices.doctors_id)
              ON doctors_practices.practice_id = practices.id
           LEFT JOIN
             (practices_services INNER JOIN services
                ON services.id = practices_services.service_id)
              ON practices_services.practice_id = practices.id')
    

    Sheesh, way past my bedtime.

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

Sidebar

Related Questions

After posting this question and reading that one I realized that it is very
after a very long sql query a have a result that is in this
Today I have question about Eclipse . I use this IDE very long and
Ok, I'm now very confused. After my last question had several people comment about
I have check very long logs after each start of Tomcat (from Eclipse). Does
This question is long winded because I have been updating the question over a
After what seems like a very long time trying to find a solution, I
I have this code: String s = A very long string containing + many
maybe this might be a simple problem but after a long while at Google
I'm using preg_match_all for very long pattern. when run the code, i got this

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.