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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:22:48+00:00 2026-06-18T15:22:48+00:00

I have a Rails app that contains a TeamSeason model class. This class has

  • 0

I have a Rails app that contains a TeamSeason model class. This class has a has_one association with a Team model class, and a has_many association to another TeamSeason called opponents. I’m now trying to write a method that passes in a Team and determines whether any of its opponents are associated with that Team. The method I wrote looks something like this:

def plays?(against_team)
  total = opponents.count {|opponent| opponent.team == against_team}
  return (total > 0)
end

The count method should count the number of array elements that yield a true value with the block I specified. However, it appears that it is always returning the full length of the array. It’s as if the block I specified always yields a true value, no matter what.

I added various puts calls to try and figure out where my logic is going wrong. Here’s my observations:

  • When I add any puts calls inside the block next to the count method, I do not see any output for those statements. It appears that the contents of the block are never being executed

  • When I insert an additional loop using the opponents array’s each method and a block, I can print the value of my array objects and confirm they are evaluate as I expect. I can even puts the value of opponent.team == against_team and verify that the block I wrote evaluates to false some of the time, as it should.

What am I missing here?

  • 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-18T15:22:50+00:00Added an answer on June 18, 2026 at 3:22 pm

    opponents is not a standard ruby Array – it’s an ActiveRecord association proxy and behaves differently for certain methods. count will query the database for the number of opponents, and the block you are passing will not be evaluated. A simpler way to do what you want is like so:

    def plays?(against_team)
      opponents.joins(:team).where(teams: {id: against_team.id}).exists?
    end
    

    This will ask the database what you want, and avoid loading all opponents when you are only checking for a specific match. Alternatively, you can load the entire opponents list and use any?:

    def plays?(against_team)
      opponents.any?{|opponent| opponent.team == against_team.id}
    end
    

    Note that this will not only load all opponents, but it will load each opponent’s team one at a time – resulting in an N+1 query (which leads to performance issues). You can avoid this by eager loading the associated team using includes():

    opponents.includes(:team).any?{...}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails app that pulls in music from Soundcloud. This data contains
I am trying to create form that contains another model in rails. I have
I have a Rails app with a user model that contains an admin attribute.
I have a rails app that has asynchronous processing, and I'm having trouble getting
We have a Rails app that instantiates a SWF object 16 times (it has
I have a rails app that I'm moving to another server and I figure
I have a rails app that has a private component and a public component.
I have an Ajax form in my Rails app that contains the Recaptcha markup
So I have a Rails 3.1 app that contains nested resources: resources :projects do
I have a trip model that contains an array of lat/lng pairs class Trip

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.