Tournament -> Team -> Player Associations - ruby-on-rails

I'm a relative beginner to Rails, but am learning as I go. I'm trying to create a Tournament Entry portal, where a team would enter players for a given tournament. I've done a bit of reading about associations, but am having some trouble wrapping my head around how to apply them in this instance.
As a basic overview:
One tournament, has many teams.
Each team has many players
Therefore one tournament also has many players (through the teams
entered)
Here's my code for this, but I'm not sure it's right because I'm unable to get any tournament_ids associated to players.
(tournament.rb)
class Tournament < ApplicationRecord
has_many :teams
has_many :players, :through => :teams
end
(team.rb)
class Team < ApplicationRecord
belongs_to :tournament
has_many :players
end
(player.rb)
class Player < ApplicationRecord
has_one :team
has_one :tournament, :through => :team
end
Within the Players table there is both team_id & tournament_id fields, however I'm only able to populate the team_id field through association when I try in console.
I'm wondering if there's something amiss with my associations.

The usage of 'belongs_to', 'has_many', 'has_one' depends on the data model in database of course.
If you have team_id foreign key in players table, then you need to define Player class as:
class Player < ApplicationRecord
belongs_to :team
has_one :tournament, :through => :team
end
In addition, I really believe that Tournament <-> Team should have many-to-many association (if team can participate in many tournaments of course). I would suggest adding model TeamTournament and define final model structure as:
class Tournament < ApplicationRecord
has_many :team_tournaments
has_many :teams, :through => :team_tournaments
has_many :players, :through => :teams
end
class TeamTournament < ApplicationRecord
belongs_to :team
belongs_to :tournament
end
class Team < ApplicationRecord
has_many :team_tournaments
has_many :tournaments, :through => :team_tournaments
has_many :players
end

the Player class should have belongs_to associations with Team and Tournament
class Player < ApplicationRecord
belongs_to :team
belongs_to :tournament
end

OK. I assume your question is about your models associations rather than how to set up association for getting tournament_id from player and so on. So I'll try to hand you some tips about your project and associations could be set up for it.
As I got your portal idea... You want the tournament to has many teams and the team to has many players. But then you want to get tournament_id from player. I believe you don't want to do that because in real life tournament indeed may "has" some players but every single player don't has to belong to some tournament. He can take part in many tournaments. So you don't need to set up association for that. Same thing with tournament and teams. But since team has the player he has to belong to that team. So you need association for that.
Wrapping up my setup for you will be like:
(tournament.rb)
class Tournament < ActiveRecord::Base
has_many :teams
end
(team.rb)
class Team < ActiveRecord::Base
has_many :players
end
(player.rb)
class Player < ActiveRecord::Base
belongs_to :team
end
And an example about how you can get the tournament where certain team take part in without the direct association:
team = Team.first # just take some team
Tournament.includes(:teams).where(teams: { id: team.id })
The same way you can achieve your other goals (get the tournament certain player belongs to and so on). But such cases don't need associations. Associations are needed when the object relates to another conceptually.

Related

Using two has one relationships to the same model

I'm trying to find the best way to model a game in relation to teams.
The end goal is to be able to call things like:
#game.winner
#game.loser
#team.games
The first two relations are working, but the games one is not. Using has_many (see below), I get ERROR: column games.team_id does not exist which I would normally work around by using whatever the equivalent to :foreign_key => winner_id, but how can I have it checkout both winner_id and loser_id?
Is the only option to create a method in the teams model like so:
def games
won = Game.where(:winner => id)
lost = Game.where(:loser => id)
won + lost
end
So far what I'm doing is:
class Game < ActiveRecord::Base
has_one :winner, class_name: "Team"
has_one :loser, class_name: "Team"
end
class Team
has_many :games
# or something that actually works
end
You didn't say much about the problem. But I think you are working too hard. If a team can play several games and a game includes more than one team, then you you need a many to many relation. This requires a third table, and best practice is a has_many :through relation. It will look something like:
class Game < ActiveRecord::Base
has_many :teams, through: assignment
has_one :winner, class_name: 'Team', through: :assignment, order: 'score DESC'
has_one :loser, class_name: 'Team', through: :assignment, order: 'score ASC'
end
class Team
has_many :games, through: :assignment
end
class Assignment < ActiveRecord::Base
belongs_to :game
belongs_to :team
end
Now you have the winner and loser attributes, but you don't need to roll your own method to count games for a team. Just say team.games.count and similarly game.teams is the teams assigned to the game.

Best method of many to many database?

I am currently working on an app for sporting clubs, this allows club admins to manage the divisions, teams, and ultimately the players.
At present I have my database/relationships as follows
class Sport < ActiveRecord::Base
has_many :clubs
has_many :teams
attr_accessible :club_id
class Club < ActiveRecord::Base
belongs_to :sport
has_many :teams
has_many :users
has_many :divisions
attr_accessible :sport_id
class Division < ActiveRecord::Base
has_many :teams
belongs_to :club
attr_accessible :club_id
class Team < ActiveRecord::Base
has_many :users
has_many :schedules
belongs_to :division
belongs_to :club
attr_accessible :division_id, :club_id
class User < ActiveRecord::Base
belongs_to :club
belongs_to :team
attr_accessiable :club_id, :sport_id
essential what I would like is a more concise way of managing this. i.e.
User can only belong to 1 club, within 1 division, but can have multiple teams
Team can only belong to 1 division, within 1 club, within 1 sport, but have multiple users
Division can only belong to 1 club, within 1 sport, but have multiple teams
Club can only belong to 1 sport, but have multiple teams, and multiple divisions
At present the above is working, but i dont think the relationships/structure is at its best
You only have singular sport clubs in your country? Here in Germany different divisions of a club can have different sports (my own club e.g. is in Athletics, Cycling, Volleyball, Basketball, Tabletennis and Swimming). In most of my apps I just skip the sport and the divisions: The sport is given from the app's context, and I view club and division as identical. My Athletic-apps just deal with the club, not with the club's division. I view this sacrifice as necessary, as the complexity otherwise overwhelms me – your mileage may vary, of course :-)
Moreover: A user only temporarily belongs to a club, so I have an intermediate relationsship between users and the teams; "Startberechtigung" in german, something like "license" in english I 'ld say; it belongs_to team and belongs_to user and has "start_date" and "stop_date" as additional attributes. That way I even have histories for clubs and users. Important for record lists (again: Athletics)!
After that I would throw away the superfluous transitive relationsships: A user belongs to a team which in turn belongs to the club => use has_many :through's more; you dont need the user belongs_to club.
Now your models should be a bit cleaner.
Update:
Dont use classname "User" for the players, this will ultimately lead to a conflict with your user/admins-model. "Player" or "Athlete" is just fine for me.
Update 2:
You dont need the :through in each and every case, of course. A scope with includes or a class method might be preferable for you, esp. if you want to bridge over more than one intermediate class.
How about something like (please fill the holes from the recommended guide):
class Sport < ActiveRecord::Base
has_many :clubs
has_many :teams, :through => :clubs, :readonly => false
class Club < ActiveRecord::Base
belongs_to :sport
has_many :teams
class Team < ActiveRecord::Base
belongs_to :club
has_many :athlete_teams
has_many :athletes, :through => :athlete_teams, :readonly => false
class AthleteTeam < ActiveRecord::Base
belongs_to :teams
belongs_to :athletes
class Athlet < ActiveRecord::Base
has_many :athlete_teams
has_many :athletes, :through :athlete_teams, :readonly => false
Help about ActiveRecord Associations can be found on http://api.rubyonrails.org
hopefully, this will help you.

Planning a Rails application associations

I'm in the process of trying to develop my first rails application and before I jump off into actually implementing my ideas, I was hoping I could get some help with my association planning.
My application is going to be an educational tool and I have some basic functionality that I need to implement. I want to have students that can register for and create courses. Within these courses, there will be one user who is the teacher and the rest are students. Within the course will be assignments that the teacher creates and that users are required to make a submission for. These are the basics. Eventually I want to add more functionality but as of now I just need the foundations set.
Here are my ideas so far on what the associations should look like:
class User < ActiveRecord::Base
has_many :enrollments
has_many :courses, :through => :enrollments
end
class Course < ActiveRecord::Base
has_many :enrollments
has_many :users, :through => :enrollments
end
class Enrollment < ActiveRecord::Base
belongs_to :user # foreign key - user_id
belongs_to :course # foreign key - course_id
end
However, I'm running into my wall of inexperience at the moment with how to appropriately handle the rest of the associations at this point. I could probably hack out something, but I'd prefer to do it as best as I can the first time.
How do I handle the associations related to assignments and submissions? Presumably a student's submission should belong_to them, but it is also specifically related to an assignment within the class. Assignments originate within a course, but are also closely tied to the user.
Finally, what's the best way to handle the relationships between a user and the class they create? A user creates a course. Does that course belong_to them? Should I just add a column to the course's table for storing the creator's id? Etc.
Thanks for the help.
Suggestion:
You might want to separate out your Teacher and Student models, since you're very likely to have different actions associated with each (and while they share some attributes, they really are different entities in your model, for example, you likely want just one teacher teaching in a course.)
You could derive both the Teacher model and the Student model from a User model that has the shared attributes and authentication.
Now to your questions:
If you'd like to keep the student that created the course associated, creator_id is the way I'd go. (If a teacher can create a course too, deriving Student and Teacher from a shared User model would help)
Assignments and Submissions:
You've pretty much defined it in words. Here is the code.
[If different students within a course could get different assignments, only then do you want to build a direct association between Student and Assignment, otherwise, use a through association]
class User < ActiveRecord::Base
has_many :enrollments
has_many :courses, :through => :enrollments
has_many :assignments, :through => :courses
has_many :submissions
end
class Course < ActiveRecord::Base
has_many :enrollments
has_many :users, :through => :enrollments
has_many :assignments
end
class Enrollment < ActiveRecord::Base
belongs_to :user # foreign key - user_id
belongs_to :course # foreign key - course_id
end
class Assignment < ActiveRecord::Base
belongs_to :course
has_many :submissions
end
class Submission < ActiveRecord::Base
belongs_to :assignment
belongs_to :user
end

Rails Nested Forms - One to One with One to Many nested

I have a problem and I have a bit of a hard time explaining it. Here are my models:
class Company < ActiveRecord::Base
has_one :publisher
has_one :developer
end
class Publisher < ActiveRecord::Base
belongs_to :company
has_many :games
end
class Developer < ActiveRecord::Base
belongs_to :company
has_many :games
end
class Game < ActiveRecord::Base
belongs_to :publisher
belongs_to :developer
end
Basically a Company can be a Developer or Publisher or both. Then I can call Company.publisher.games to see all the games the company published, and Company.developer.games to see the games they developed.
The problem is I don't know the most efficient way to nest the forms for this.
Basically when I am at /company/new, I want to have a form for the company, which in turn has a box where I can specify multiple game IDs for either the Publisher or the Developer aspect of the company. I can't seem to figure out how I can just do #company.update_attributes(params[:company]) and have that take a nested form and create a publisher and or developer with multiple associated games.

User and Team modeling

I have been doing some searching around Google and this site for some ways to go about constructing some models for an application I am working on.
I plan to have Users and Teams. A user can join a team and one team only. A team can obviously have many users. Also, I want to add a 'leader' role inside the team that is assigned to the User who creates a Team but can later be passed on to another User who is already a part of that team.
I have seen some examples of models for something like this and saw a few like this:
class User < ActiveRecord::Base
has_one :team
has_many :memberships
end
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :team
end
class Team < ActiveRecord::Base
has_many :memberships
has_many :users, :through => :memberships
end
Why is the Membership model necessary? I have seen it in a few examples and I am not sure I quite follow the logic of why it is in there and what it is doing.
Also, if I did something like has_one :leader in the Team model and had the Leader model, would that be considered the best practice for determining a leader in a Team? It seems like a bit much to have an entire model/table for Leader.
The Memberships model is allowing for a many-to-many relationship there. It's acting as the join table. It would allow Users to belong to many Teams and Teams to have many Users.
Sounds like you just want a one-to-many though?
class User < ActiveRecord::Base
belongs_to :team
end
class Team < ActiveRecord::Base
has_many :users
end
I'll have to double check this part but you can use additional parameters to specify the models in your assocation if they don't match up with the name you need. So Team could have a leader that's just a User model.
class Team < ActiveRecord::Base
has_many :users
has_one :leader, :class_name => "User", :foreign_key => "user_id"
end
the rails guides page on associations has a good summary including the part about the :class_name and other options
http://guides.rubyonrails.org/association_basics.html#the-has_many-association

Resources