Has_many association with specific values: how-to? - ruby-on-rails

I want to build a (toy/learning) RoR app for managing personal boardgame library, but I'm stuck when I try to define a correct data structure.
Basically, a user owns a library with many games. For each game, the user defines a price and a rating.
So I have a simple model for user, with name:text attribute and a model for game, with title:text attribute.
I also have a library model, that references the user model, and have a one-to-many relationship with games.
Now I'm not sure how to define the attributes rating and price for the games on the library.
Simple idea is to define report as join table from library and games with attributes rating and price, but I feel this solution so... clumsy.
Any other ideas?

No clumsy at all. It is the right solution, indeed.
You need a many-to-many association between library and game since a library can have many games and a game can be in many libraries.
As you said, create the model report referencing both library and game plus the columns rating and price, then define the has_many :through associations:
class Report < ApplicationRecord
belongs_to :library
belongs_to :game
end
class Game < ApplicationRecord
has_many :reports
has_many :libraries, through: :reports
end
class Library < ApplicationRecord
belongs_to :user
has_many :reports
has_many :games, through: :reports
end

Related

Tournament -> Team -> Player Associations

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.

Associating 3 Models in Rails

I'm a little confused on which type of association I should set up in my application.
I have these three models: Booking, Availability and Facility
A facility puts up 1 hour blocks of available time. So I'd say Availability: belongs_to :facility.
I'm trying to represent that a booking could have many availabilities. In that if I book from 1-3PM that's two 1 hour blocks and I'd like for that to be represented in 1 record in the Bookings table.
I was thinking I should set up Bookings: has_many :availability
but then I was reading about has_many though and I wasn't sure if it would be more appropriate to do Facilities has many Bookings through Availability...?
I would absolutely do a has_many :through association here, but I'd make the association between Availability and Booking slightly different than a typical has_many :through association:
class Facility < ActiveRecord::Base
has_many :availabilities
has_many :bookings, through: :availabilities
end
class Availability < ActiveRecord::Base
belongs_to :facility
has_one :booking
end
class Booking < ActiveRecord::Base
belongs_to :availability
end
The reason I prefer this association style is because in a typical has_many :through, you have two entities sharing relationships with one another through a third entity (e.g. Patients and Doctors sharing a relationship through an entity called Appointment, as explained by the Rails guides). This example is different, however, in that Booking shouldn't hold any positive relationship to Facility--the relationship should merely be a by-product of an open Availability.

Layers of associations and descriptions on rails

I am trying to implement a resume feature and I was wondering the best practice for handling it. Lets assume a user has 2 pieces to a resume, education and work experience, and this is constant among all users. Then under each I want to save titles of things they've done (eg. Attended school at ______, worked at ________, project doing _______). Within these I want to save a description of that specific activity. What would be the best practice for implementing this?
So I need some association like: a user has_many experiences. An experience has_many titles. A title has_many descriptions. I also need to make the title of the experience is associated with the correct header (education or work experience).
I'm still pretty new to rails, and I'm sure there is a much easier and intuitive way to do this. Thanks for the help!
You can try these simple models structure for
class User < ActiveRecord::Base
has_many :locations
has_many :projects, through: experience_projects
end
class Location < ActiveRecord::Base
belongs_to :user
end
class Experience < ActiveRecord::Base
#experiences table should contain title and descriptions
belongs_to :user
has_many :projects, through: experience_projects
end
Now Join model if you do not require this intervention model you can directly use has_and_belongs_to_many
class ExperienceProject < ActiveRecord::Base
belongs_to :user
belongs_to :experience
end

How to organize seasons in Clubs and Teams for Rails models?

I have the following situation:
I have a Club which can have several Teams, and a Team can have several Players.
However, the Players that a Team has will vary each year. I am wondering how could I put somehow season information into the picture in a way that the user will be able to add/remove Players each season and be able to see Players of a team from previous seasons.
So, given the following models:
Club has_many Team
Team belongs_to Club
Team has_many Players
How would I make it so that Team has_many Players depends on the Season (2013/2014 - 2012/2013..)
There are two ways to do it:
One is to create another relation for the SEASON and create a ternary relationship (http://www.databasedesign.co.uk/bookdatabasesafirstcourse/chap3/chap3.htm)
The tables would look something like this:
PLAYER(id,...)
TEAM(id,name,...,club_id)
SEASON(id,season_name,...)
SEASON_PARTICIPATION(id,player_id,season_id,team_id)
And the ActiveRecord associations would be:
class Club < ActiveRecord::Base
has_many :teams
end
class Team < ActiveRecord::Base
belongs_to :club
has_many :players, through: :season_participations
end
class Player < ActiveRecord::Base
belongs_to :teams, through: :season_participations
end
class Season < ActiveRecord::Base
has_many :players, through: :season_participations
end
class SeasonParticipation < ActiveRecord::Base
belongs_to :player
belongs_to :team
belongs_to :season
end
More documentation on ActiveRecord associations can be found here: http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
Another way of doing this is to skip the SEASON relation all together and have it as a simple attribute in the SEASON_PARTICIPATION relation. So instead of a season_id you would have the name of the season in the SEASON_PARTICIPATION (see diagram 2). Having a separate SEASON relation gives you the opportunity to store more information about the season, like start-date/end-date etc. Eventually you could also add division information in another relation.
There's many ways you could do this. Here's a few that spring immediately to mind.
Add a season column to the Player model. Have the Player's season attribute be set on create via callback. If the season != current_season, prevent the user from creating, updating, or destroying the player.
Use the updated_at column which is already provided in the Player model to do basically the same thing. Create a method which parses the datetime and converts it into something more useful to you, probably a season number or something along those lines.

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

Resources