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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:40:18+00:00 2026-06-06T16:40:18+00:00

How does one query for lateral grandparent in Ruby on Rails? For instance I

  • 0

How does one query for lateral grandparent in Ruby on Rails?

For instance I have the following models setup:

User

class User < ActiveRecord::Base
   has_many :registrations
end

Registration

class Registration < ActiveRecord::Base
   belongs_to :user
   belongs_to :competition
end

Competition

class Competition < ActiveRecord::Base
  has_many :registrations
  belongs_to :tournament
end

Tournament

class Tournament < ActiveRecord::Base
  has_many :competitions
end

So what I want to do is find all Users who have Registrations belonging to a particular Tournament. The only way I know of to do this is by finding all users, looping through each of them and then querying each users individual registrations like so…

<% @users.each do |user| %>
    <% if user.registrations..joins(:competition).where("competitions.tournament_id = #{params[:tournament]}").count > 0 %>
        print out user information...
    <% end %>
<% end %>

This seems like a real half-youknowwhat way of doing things because I’m selecting records that I don’t need, can anybody suggest a better more Railsy way of accomplishing this?

  • 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-06T16:40:19+00:00Added an answer on June 6, 2026 at 4:40 pm

    The first step I would take to create a more “Railsy” solution would be to move the query logic out of the view and into the model layer. You could add these two queries to your Tournament model:

    class Tournament < ActiveRecord::Base
      has_many :competitions
    
      def registrations
        regs = []
        self.competitions.each do |competition|
          competition.registrations.each do |registration|
            regs << registration
          end
        end
        regs
      end
    
      def users
        usrs = []
        self.registrations.each do |registration|
          usrs << registration.user
        end
        usrs
      end
    
    end
    

    Now within a view you can:

    <% @tournament.users.each do |user| %>
      User: <%= user.name %><br />
    <% end %>
    

    You also have a method that returns all the registrations in a tournament, should you ever need it. Hope this helps you see another approach.

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

Sidebar

Related Questions

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent =>
Say I have the following models: class Baz(models.Model): winning = models.CharField(max_length=100) class Bar(models.Model): baz
I have a question: Does it possible to collect multiple query in one query?
How does one handle ties when ranking results in a mysql query? I've simplified
I have one query regarding the (UITextfield and Webservice) for example When i start
One would normally have this query in their sphinx.conf file : sql_query = SELECT
Possible Duplicate: TSQL query to concatenate and remove common prefix Hi How does one
How does one parameterize variable input into a Regex in Ruby? For example, I'm
I have a stored proc that does a geospatial query. The proc issues two
Using Linq to SQL How does one query and match to include all rows

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.