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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:59:57+00:00 2026-06-14T16:59:57+00:00

UPDATE 2: This looks much better: Comp.includes(:members).where(‘members.member_email = ? OR comps.user_id = ?’, current_user.email,current_user.id)

  • 0

UPDATE 2:

This looks much better:

Comp.includes(:members).where('members.member_email = ? OR comps.user_id = ?', current_user.email,current_user.id)

UPDATE:

This seems to work but is there a more elegant way to do this in Rails? I feel like there must be.

@my_comps = Comp.joins('LEFT OUTER JOIN teams ON teams.comp_id = comps.id LEFT OUTER JOIN members ON members.team_id = teams.id').where('members.member_email = ? OR comps.user_id = ?', current_user.email,current_user.id).group('comps.id')

ORIGINAL:

My model associations are:

Comp.rb
    has_many :teams
    has_many :members, :through => :teams

Team.rb
    belongs_to :comp
    has_many :members

Member.rb
    belongs_to :team

I want to write a query that finds all of the Comps where comps.user_id equals a particular value OR members.member_email equals a particular value for any of the members of that Comp.

I unsuccessfully tried this:

@my_comps = Comp.joins(:members).where('members.member_email = ? OR comps.user_id = ?', email, id)  

There are 2 issues with the results returned: 1) it returns duplicate Comps where member_email is equal to the condition and 2) it does NOT return the Comps where the user_id is equal to the condition. I solved problem 1 by adding .group(‘id’) to the end of this code but I feel like there is likely a better way to do it, and more importantly it doesn’t solve problem 2.

Any advice on how to approach this differently? Thanks so much.

  • 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-14T16:59:58+00:00Added an answer on June 14, 2026 at 4:59 pm

    changed the suggestion, didn’t know that .join only uses “INNER JOIN” in newer Rails (having an old version).
    The final suggestions was: use .include instead of .join

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

Sidebar

Related Questions

UPDATE (spoiler): This question is answered (see David Carlisle answere below) and it looks
UPDATE It looks like this problem has been quietly fixed in iOS 4.3. Up
Update As suggested by many people, it looks like this was because of the
I have an if statement that needs to look like this: UPDATE $(input#textbox).keypress(function(e){ key==e.which;
This is what it looks like: artifact org.springframework:spring-core: checking for updates from glassfish-repository WARNING]
Update : This is no longer an issue from C# 6, which has introduced
Update: This does work, I was being stupid :( i have the following extension
Update: This is, as I was told, no principle Python related problem, but seems
UPDATE: this is a repost of How to make shell scripts robust to source
UPDATE: This question is out of date, but left for informational purposes. Original Question

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.