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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:53:43+00:00 2026-05-29T05:53:43+00:00

I am not sure how to figure this one out, because I dont know

  • 0

I am not sure how to figure this one out, because I dont know how I would update my intermediary table, without having to do delete_all. Or where to do inserts and deletes, etc.
I have three classes:

class User < ActiveRecord::Base
  has_many :players
  has_many :teams, :through => :players 
end

class Team < ActiveRecord::Base
  has_many :players
end

class Player < ActiveRecord::Base
  belongs_to :team
  belongs_to :user
   (contains other columns like, is_captain, has_paid, etc.)
end

I am displaying a page with checkboxes and you can select which ‘users’ you would like to be on your team (from your AddressBook). So imagine 25 names listed, and some of them are already checked and others are not. How could I save the data of checkboxes, cause they may have 3 different states: 1) insert brand new player (insert row into player) 2) remove player from team (delete existing player) or 3)do nothing (a player you dont want on your team)

The issue i am running into is that when you ‘uncheck’ a box, then the front-end does not send that to back-end? Any ideas how this can be done nicely?

  • 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-05-29T05:53:45+00:00Added an answer on May 29, 2026 at 5:53 am

    Not sure if this is the best way to do this but it works, I am open to recommendations in improving this code

        params[:team][:user_ids] ||= []
        @team = Team.find(params[:team][:id])
        current_players = @team.players
        current_players_user_ids = current_players.collect { |player| player.user_id }
        user_ids = params[:team][:user_ids]
        puts "input ids #{user_ids}"
        #add current_user because checkbox does not pass them in since its 'disabled'
        user_ids << current_user.id
    
        #delete the current_players that are not in the the received 'checkboxes'
        current_players_user_ids.each do |current_player_user_id|
          unless user_ids.include?(current_player_user_id)
            Player.delete_all(:team_id => @team.id, :user_id => current_player_user_id)
          end
        end
    
        #iterate through input ids and run 'inserts' on those who are not current-players
        user_ids.each do |user_id|
          unless current_players_user_ids.include?(user_id)
            player = Player.new(:user_id=>user_id, :team_id=>@team.id)
            @team.players << player
          end
        end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure why I can't figure this out. When a user makes a selection,
Not sure why I'm getting this error and can't figure it out? The cycle
I figured this would be simple, but being new to SSRS I'm not sure.
I'm not sure if this is exactly possible, but figured I'd throw it out
In Ruby 1.9.3 (and probably earlier versions, not sure), I'm trying to figure out
I've been looking through related LINQ questions here trying to figure this one out,
Not sure whats going on here, or what could be the integer in this
Not sure how to ask this, but i'll give it a try: I have
Not sure if this is a big deal. But wondering why when the site
I am not really sure what to do in this situation. I have some

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.