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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:33:51+00:00 2026-06-01T12:33:51+00:00

I’ve got these models class PlayersToTeam < ActiveRecord::Base belongs_to :player belongs_to :team accepts_nested_attributes_for :player

  • 0

I’ve got these models

class PlayersToTeam < ActiveRecord::Base
  belongs_to :player
  belongs_to :team

  accepts_nested_attributes_for :player
end

class Player < ActiveRecord::Base
  has_many :players_to_teams
  has_many :teams, through: :players_to_teams
end

class Team < ActiveRecord::Base
  has_many :players_to_teams
  has_many :players, through: :players_to_teams
  belongs_to :account
end

In my show view for teams, I show all the players on that team. The edit link actually goes to edit the players_to_teams entry, so I had something like this:

  <% @team.players.each do |player| %>
  <tr>
    <td><%= player.FirstName %></td>
    <td><%= player.LastName %></td>
    <td><%= link_to "Edit", edit_players_to_team_path(player.players_to_teams.find_by_team_id(@team.id)) %></td>
  </tr>

where @team was defined as Team.find(params[:id]). This was SUPER slow, and when looking at the dev logs, it was because the db was being hit numerous times for every player at the edit_players_to_team_path line (to find the player, then to find the player_to_team that matched the requirements, maybe more?).

So I switched it instead to use the players_to team record

<% @players_to_teams.each do |ptt| %>
  <tr>
    <td><%= ptt.player.FirstName %></td>
    <td><%= ptt.player.LastName %></td>
    <td><%= link_to "Edit", edit_players_to_team_path(ptt) %></td>
  </tr>
  <% end %>

where @players_to_teams is eqaul to team.players_to_teams in the controller. This goes a lot faster, but still seems to be hitting the db for every row in my view.

I’m guessing Team.find(params[:id]) doesn’t return the players or players_to_teams records that are associated with that team. Is there a way I can include those associations so that a call to Team.find(params[:id]) returns an object with references to both the player and player_to_teams associated records, so the db only gets hit once?

  • 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-01T12:33:53+00:00Added an answer on June 1, 2026 at 12:33 pm

    When retrieving the @player_to_teams records, eager load the players in that query using .include(:player) (actual syntax may vary depending on the query you are using). That way the app will make 1 call to the database to get the ptt records and possibly one more call to get all the players. This will avoid having to lookup the player for each ptt iteration in the view. Here’s a link that demonstrates – http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string

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.