1

I'm making a fantasy soccer team web application to learn how to use ruby on rails and I'm a bit confused regarding how relational databases should work with this sort of example.

I have a table for Users, Players, Teams and My_Team.

class Player < ActiveRecord::Base
    belongs_to :team
    has_one :team
    has_and_belongs_to_many :my_team 
end

class Team < ActiveRecord::Base
    has_many :players
end

class MyTeam < ActiveRecord::Base
    belongs_to :user
    has_many :players
end

These are the models I've made. I'm wondering if I need a new class to tie them all together? I'm not quite sure how to link them - I haven't made anything requiring a relational database model in a while!

I've successfully displayed all the data, and I have some dummy date in place for players and teams but how should I carry on so that one user has one user team, which has many players?

Is this right?

I'm trying to add a player to someones personal team but I feel like it's not working because of the table design..

1
  • Time to follow a published academic textbook on information modelling, the relational model & DB design & querying. (Manuals for languages & tools to record & use designs are not such textbooks.) (Nor are wiki articles or web posts.) Commented Oct 11, 2019 at 7:45

2 Answers 2

7

I'm trying to add a player to someone's personal team but I feel like it's not working because of the table design

ob264 is somewhat right:

#app/models/user.rb
Class User < ActiveRecord::Base
   has_many :teams
   has_many :players, through: :teams
end

#app/models/team.rb
Class Team < ActiveRecord::Base
   #fields id | user_id | other | team | details | created_at | updated_at
   belongs_to :user

   has_many :player_teams
   has_many :players, through: :player_teams
end

#app/models/player.rb
Class Player < ActiveRecord::Base
   #fields id | team_id | other | team | details | created_at | updated_at
   has_many :player_teams
   has_many :teams, through: :player_teams
end

#app/models/player_team.rb
Class PlayerTeam < ActiveRecord::Base
    belongs_to :team
    belongs_to :player
end

This will allow you to create the following setup:

#config/routes.rb
resources :teams #-> domain.com/teams/5/edit

#app/controllers/players_controller.rb
Class TeamsController < ApplicationController
   def edit
       @team = Team.find params[:id]
       @team.player_teams.build
   end

   def update
       @team = Team.find params[:id]
       @team.update(team_params)
   end

   private

   def team_params
      params.require(:team).permit(:team, :params, player_teams_attributes: [:player_id, :team_id])
   end
end

Although this will need tweaking, it will basically give you the ability to do this:

#app/views/teams/edit.html.erb
<%= form_for @team do |f| %>
   <%= f.fields_for :player_teams do |pt| %>
      <%= pf.collection_select :player_id, Player.all, :id, :name %> #-> adds a player to your team
   <% end %>
   <%= f.submit %>
<% end %>

--

ActiveRecord Associations

To answer your question more deeply, you have to appreciate the role of ActiveRecord associations that Rails uses. ActiveRecord is an "ORM" (Object Relational Mapper) layer for data-driven applications, allowing the application-layer to build associated objects together.

The difference here is that if you want to create "relations" / "associations" in your dataset, you have to do several important things:

  1. You need to appreciate the role of foreign_keys in your data tables
  2. You need to associate your models correctly
  3. You need to work at object level

Firstly, you need to appreciate that ActiveRecord is not magic - it build objects based off the foreign_key in your data tables. This is typically a reference to the primary_key of the parent object:

enter image description here

This means that every time you build an association in Rails, you need to back it up with the data table foreign_keys it needs to find the respective data.

Secondly, you need to then ensure your models are associated correctly. The examples provided are a sufficient demonstration of how this should work (simple once you get your head around it!)

Thirdly, you need to the appreciate working at object level. This is very important - Rails (as it's built on Ruby) is an object orientated framework, meaning that everything you do is based around objects (which are constructed through the Models, from the database)

I am saying this because the way I built the methodology for you above is object orientated -- you're wanting to update a user's "Team" object by adding / removing players from the association. This is a very important pattern to realize within Rails - it will help you profusely if you appreciate how it works

Sign up to request clarification or add additional context in comments.

Comments

4
class Player < ActiveRecord::Base
    belongs_to :team 
end

class Team < ActiveRecord::Base
    has_many :players
    belongs_to :user
end

class User< ActiveRecord::Base
    has_one :team
    has_many :players, through: :team
end

This structure allows each user to have a team which has many players

It also allows you to access each of the elements by

get users team: @user.team

get teams user: @team.user

get users players: @user.players

get players user: @player.user

get teams players: @team.players

get players team: @player.team

Hope this helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.