Ask A Question


You’re not receiving notifications from this thread.

Save Each Collection Check Box on A Form as Its Own Object

Kevin Hemphill asked in Rails


I'm fairly new to Rails and I'm working on a pretty large, fairly complicated project for a friend as a way to challenge myself. (Hey, go big or go home right?).

A little background: The project is essentially an horse riding competition event management platform from scratch that can manage everything from registrations to scoring at the event.

The expected flow of this is as follows:

  • User signs up for the platform and creates a profile.
  • Inside the platform are multiple shows (added by an admin).
  • Each show has multiple events.
  • Each event within a show has multiple divisions (declared when creating the show).
  • User (after signing up) logs in and clicks on a show they want to register for.
  • On the registration form for the show: --- They check the boxes for each event they want to participate in for that show. *** --- For each event they select, they choose what division of that event they want to participate in from a drop down. --- In a text box they enter the name of the horse they plan on riding for each event within that show.
  • On the day of the show: --- Each division of each event has a score "page" that pre-populates who registered for that event's division along with text boxes for an admin to enter scores each participant receives.

I have the following models created:

  • user.rb (created using Devise gem)
  • show.rb
  • division.rb
  • event.rb
  • register.rb
  • score.rb (to be created)

Here are my current association models:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  has_many :shows, through: :registers
  has_many :events, through: :registers
  has_many :divisions, through: :registers
  has_many :registers


class Show < ApplicationRecord
  has_many :divisions, dependent: :destroy
  has_many :events, dependent: :destroy
  has_many :registers
  has_many :users, through: :registers


class Division < ApplicationRecord
  belongs_to :show
  belongs_to :event, :optional => true
  has_many :registers
  has_many :users, through: :registers


class Event < ApplicationRecord
  belongs_to :show
  has_many :registers
  has_many :users, through: :registers
  has_many :divisions


class Register < ApplicationRecord
  belongs_to :show
  belongs_to :user
  has_many :events
  has_many :divisions

The *** in the workflow is where I am stuck at. I feel like the best way to make the score pages work is to have each selected event check box save as it's own object to the "registers" db, which is why I haven't created the scores model yet until I figure this out. I'm thinking it's probably an association problem and that I might not be thinking through my associations correctly. Associations has been a bit of a murky subject for me to grasp appropriately.

Any help/feedback/thoughts are welcome. Also, please let me know if I need to share any controllers, forms, etc. to help better help or understand the issue. Thanks so much!

P.S. - Please be gentle with comments as, like I stated before, I'm still learning the ins and outs of Rails. :-)

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 73,895+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.