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.
Related
I'm uncertain of the best approach to model conversation relationships.
My application has Users, Conversations, UserConversations, Venues, & VenueConversations.
Users can have many Conversations & Conversations can have many Users. Thus, the bridging table (UserConversations).
Venues can also have Conversations and these Conversations can also have many Users. Thus, the bridging table (VenueConversations)
I've tried it with polymorphic associations but then Messages in the Conversations became difficult to manage. Also, a while back I asked a question on Stack and someone said I should steer clear of polymorphic associations at all costs.
I hope to do this properly so that querying doesn't become a nightmare in the future.
I'd also like to state I'd like to have one Conversation model because one day they'll share a lot of behavior.
User.rb
class User < ApplicationRecord
has_many :user_conversations
has_many :conversations, through: :user_conversations
has_many :venue_conversations
has_many :conversations, through: :venue_conversations
end
UserConversation.rb
class UserConversation < ApplicationRecord
belongs_to :user
belongs_to :conversation
end
Venue.rb
class Venue < ApplicationRecord
has_many :venue_conversations
has_many :conversations, through: :venue_conversations
end
VenueConversations.rb
class VenueConversations < ApplicationRecord
belongs_to :user
belongs_to :venue
belongs_to :conversation
end
The problem I'm having now is that when I do User.first.conversations I only find one of the types of conversations a User could have(Private vs Venue). It reflects the last definition I gave to has_many :conversations in the User.rb model. Perhaps a scope is in order? How would I make a distinction between private & venue conversations as well?
Please advise me on how I could resolve this or take a better approach
I have a model Company which belongs to model Portfolio. A portfolio can have multiple arrays of companies i.e. Sponsoring company list, Owning Companies list etc.
How can I use the has_many relations to include multiple instances of Company lists but call them by different names, as they are different fields but of the same type?
I would recommend using this
class Portfolio < ActiveRecord::Base
has_many :portfolio_companies
has_many :companies, :through => :portfolio_companies
end
class Company < ActiveRecord::Base
has_many :portfolio_companies
has_one :portfolio, :through => :portfolio_companies
end
class PortfolioCompany < ActiveRecord::Base
belongs_to :portfolio
belongs_to :companies
end
With the help of above relationship you can use different kind of of type object Its may be has many or has one
You can define different field to associate it
my data is currently structured in the following way.
STATE (ie California) "has_many" SCHOOLS,
SCHOOL "has_many" GRADES (ie 8th grade or 9th grade),
GRADE "has_many" CLASSES (ie art, history, chemistry),
CLASSES "has_many" STUDENTS.
My goal is to get low-level detail from a high-level view, otherwise, I would like to get all students (lowest denominator within the data structure) within the STATE (highest level component within the structure). What's the easiest and most efficient way to structure this so I can have a command to get all students within a state, ie "State.find(name: "California").students".
I tried a two level "through" association but it seems to be limited.
A potential basic data design would be something like the following:
class State < ActiveRecord::Base
has_many :schools
has_many :students, through: :schools
end
# Links students to schools
class Enrollment < ActiveRecord::Base
belongs_to :school
belongs_to :student
belongs_to :grade
end
class School < ActiveRecord::Base
has_many :enrollments
has_many :students, through: :enrollments
has_many :grades
has_many :subjects, through: :grades
belongs_to :state
end
# Denotes a educational step as in 1st grade
class Grade
belongs_to :school
has_many :subjects
end
# We can't use Class since it clashes with the Class object.
class Subject
belongs_to :grade
end
class Student < ActiveRecord::Base
has_many :enrollments
has_many :schools, through: :enrollments
end
State.find(1).students would join through School and Enrollment.
Flattening the structure would require a states_students join table which is pretty unsexy due to data duplication. The duplication means more slow insert and update queries when creating / updating students. The same applies to subjects and grades.
This does not address differentiating between current and former students which could be solved by adding an end date or a enum denoting the status to Enrollment.
Enougher issue is dealing with data that might be shared by all Grades or Classes. Like for example goals that would be setup on a state level and not on a school level. Another case is a subject which might stretch though several grades.
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.
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