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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:22:20+00:00 2026-06-06T08:22:20+00:00

I have a relationship between two models, Registers and Competitions . I have a

  • 0

I have a relationship between two models, Registers and Competitions. I have a very complicated dynamic query that is being built and if the conditions are right I need to limit Registration records to only those where it’s Competition parent meets a certain criteria. In order to do this without select from the Competition table I was thinking of something along the lines of…

Register.where("competition_id in ?", Competition.where("...").collect {|i| i.id})

Which produces this SQL:

SELECT "registers".* FROM "registers" WHERE (competition_id in 1,2,3,4...)

I don’t think PostgreSQL liked the fact that the in parameters aren’t surrounded by parenthesis. How can I compare the Register foreign key to a list of competition ids?

  • 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-06T08:22:22+00:00Added an answer on June 6, 2026 at 8:22 am

    you can make it a bit shorter and skip the collect (this worked for me in 3.2.3).

    Register.where(competition_id: Competition.where("..."))
    

    this will result in the following sql:

    SELECT "registers".* FROM "registers" WHERE "registers"."competition_id" IN (SELECT "competitions"."id" FROM "competitions" WHERE "...")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple has_one/belongs_to relationship between two models. This is a new association
I have two models, Project and Category, which have a many-to-many relationship between them.
I have the following two models: School and User, and a HABTM relationship between
I have two models Property and Amenity the relationship between them is many to
Say I have two models, Book and Author with a has_and_belongs_to_many relationship between them.
I have a simple one-to-many (models.ForeignKey) relationship between two of my model classes: class
I have two models with a foreign key relationship between them. In the admin,
I have a many-to-many relationship between two models, Lists and Users. When a user
If I have a has_and_belongs_to_many relationship between two models, let's say Users and Accounts
In my model, I have two tables with a 1:0..1 relationship between them: (Example

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.