How to set up associations in Ruby on Rails? - ruby-on-rails

I'm building a sample app for practice and am having trouble determining the best way to organize my models and associations. So let's just say I have 3 models:
Schools
Classes
Students
I want:
schools to have many classes
classes to have many students
classes to belong to a school
students to be enrolled in many classes in many different schools
The associations are making me dizzy, I'm not sure which ones to use. Help would be greatly appreciated.

Renamed class to course, as the class name Class is already taken. A join class such as enrollments would handle your many to many course <=> student relationship.
class School
has_many :courses
end
class Course
belongs_to :school
has_many :enrollments
has_many :students, :through => :enrollments
end
class Student
has_many :enrollments
has_many :courses, :through => :enrollments
end
class Enrollment
belongs_to :course
belongs_to :student
end

Your models should looks like this:
class School < ActiveRecord::Base
has_many :classes
has_many :students, :through => :classes
end
class Class < ActiveRecord::Base
belongs_to :school
has_and_belongs_to_many :students
end
class Student < ActiveRecord::Base
has_and_belongs_to_many :classes
end
Make sure your Student and Class tables have class_id and school_id columns respectively.
Also, Class is a reserved word in Rails, so it might cause problems (you might have to use a different name)

Though on first blush it would seem students should belong directly to class, class isn't really a true "has_and_belongs_to_many" replacement. For that I would use "enrollment". (Note with rails 3.1 you can now do nested :through calls.)
Here's a slightly more advanced implementation than the previous commenter's:
class School << ActiveRecord::Base
has_many :academic_classes
has_many :enrollments, :through => :academic_classes
has_many :students, :through => :enrollments, :uniq => true
end
class AcademicClass << ActiveRecord::Base
belongs_to :school
has_many :enrollments
end
class Enrollment << ActiveRecord::Base
belongs_to :academic_class
belongs_to :student
end
class Student << ActiveRecord::Base
has_many :enrollments
has_many :academic_classes, :through => :enrollments
has_many :schools, :through => :academic_classes, :uniq => true
end

Related

Should this has_many :through relationship be polymorphic or not?

I have a many-to-many relationship setup for Teachers and Classrooms via has_many :through:
class Teacher < ActiveRecord::Base
has_many :classrooms, :through => :classroom_memberships
end
class Classroom < ActiveRecord::Base
has_many :students
has_many :teachers, :through => :classroom_memberships
end
class ClassroomMemberships < ActiveRecord::Base
belongs_to :teacher
belongs_to :classroom
end
Currently, Students can only belong to one Classroom:
class Student < ActiveRecord::Base
belongs_to :classroom
end
Now I have the need to track historical classroom memberships for students, creating a second many-to-many relationship for classrooms. So, while a student can only belong to one classroom at a time, I need to know that last year, student A belonged to classroom B.
I'm thinking I have two viable options:
1.) Make the classroom_memberships association polymorphic so I'd have a classroomable_id and classroomable_type that would point to either a teacher OR a student.
2.) Simplify things and add another foreign key to ClassroomMemberships called student_id, in which case, for a given row, either student_id OR teacher_id would have a value.
Which is the better option?
I would probably go the route of:
class Course < ActiveRecord::Base
# like "MATH 100"
has_many :sections
has_many :teachers, :through => :sections
end
class Term < ActiveRecord::Base
# like "Fall 2015"
has_many :sections
end
class Teacher < ActiveRecord::Base
has_many :sections
has_many :courses, :through => :sections
end
class Section < ActiveRecord::Base
# a course, in a term, taught by a teacher, with registered students
belongs_to :term
belongs_to :course
belongs_to :teacher
has_many :registrations
has_many :students, :through => :registrations
end
class Registration < ActiveRecord::Base
# a student in a specific section
belongs_to :section
belongs_to :student
end
class Student < ActiveRecord::Base
# a student's registrations are their course history
has_many :registrations
has_many :sections, through :registrations
end
As a start, since this is a fairly basic modeling of an educational system.
It sounds like maybe you want a ClassroomMembershipHistory model.
Something like
class ClassroomMembershipHistory < ActiveRecord::Base
belongs_to :student
belongs_to :classroom
end
with a year attribute, stored however is easiest to query for your use case.

rails polymorphic many-to-many association

I have these models:
class Company < ActiveRecord::Base
has_many :categorizings, as: :categorizable
has_many :categories, :through => :categorizings
class Categorizing < ActiveRecord::Base
belongs_to :category
belongs_to :categorizable, polymorphic: true
class Category < ActiveRecord::Base
has_many :categorizings
has_many :categorizables, through: :categorizings
How can I get all the companies having a specific category?
I tried
Category.find_by_name("fff").companies
plus a lot of other solutions but couldn't get it working
Thx!
You could do the following
Category.first.categorizables.where(categorizable_type: 'Company')
that would select all the Companies associated with Category using your relationship schema.
Also read some about has_many :through association because I think you don't quite get it http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

Rails associations - how can I set up associations for different varieties of users?

I'm creating a web app which consists of schools, courses, students, and teachers.
A school can have many courses and a course has one teacher and many students.
The problem I am running into is that a single user could be a teacher of one course, but a student in another course (or even a student or teacher in a course in a different school). I don't want to create a model for teachers and a separate model for students because I would like to track all my users in one place. There is an enrollment table which lists which users are enrolled as students in a course.
I would like to do something like the following:
class School < ActiveRecord::Base
has_many :courses
has_many :students :through enrollments
has_many :teachers :through courses
end
class Course < ActiveRecord::Base
has_one :teacher
belongs_to :school
has_many :students :through enrollments
end
class User < ActiveRecord::Base
has_many :courses
has_many :schools
end
But if I have only a users table and not two separate students and teachers tables, this won't work.
Instead, I would have to do something like
class School < ActiveRecord::Base
has_many :users [that are teachers]
has_many :users :through enrollments [that are students]
end
How can I set up my model and associations to make this work?
Thanks.
Use inheritance.
Teachers and students are inherited from the users model. You can consult http://api.rubyonrails.org/classes/ActiveRecord/Base.html for further information. Be sure to create a "type" column or equivalent in your User table.
class User < ActiveRecord::Base
end
class Student < User
end
class Teacher < User
end
Rails will treat them individually, but they will still exist in the User table.Let me know if you need further assistance
I may have missed something, but it should work if you add the class_name to your relation with "User":
class School < ActiveRecord::Base
has_many :courses
has_many :students :through enrollments, :class_name => "User"
has_many :teachers :through courses, :class_name => "User"
end
class Course < ActiveRecord::Base
has_one :teacher, :class_name => "User"
belongs_to :school
has_many :students :through enrollments, , :class_name => "User"
end
class User < ActiveRecord::Base
has_many :courses
has_many :schools
end
Add a teachers_id column to courses and use belongs_to instead of has_one. Then add a class_name option.
class School < ActiveRecord::Base
has_many :courses
has_many :students :through enrollments
has_many :teachers :through courses
end
class Course < ActiveRecord::Base
belongs_to :teacher, :class_name => 'User'
belongs_to :school
has_many :students :through enrollments
end
class User < ActiveRecord::Base
has_many :courses
has_many :schools, :through enrollments
has_many :teachers, :through :courses
end

What is the correct rails model? Using redundancy recommended?

In my rails application Company acts as the user model. A company can have many customers and many employees (carseller).
There is a many to many relation between a carseller and a customer.
There is the following problem: A lot of times i'd have to retrieve all the appointments made with the whole company. Since there is no company_id saved in the appointment model this can be quite painful.
Should i just include a foreign key to company in the appointments and have some form of redundancy or is there another easy and efficient way?
class Company < ActiveRecord::Base
has_many :customers
has_many :carseller
end
class Customer < ActiveRecord::Base
belongs_to :company
has_many :appointments
has_many :carsellers, :through => :appointments
end
class Carseller < ActiveRecord::Base
belongs_to :company
has_many :appointments
has_many :customers, :through => :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :customer
belongs_to :carseller
end
I think you can use :
class Appointment < ActiveRecord::Base
belongs_to :customer
belongs_to :carseller
has_one :company, :through => :carseller
end
Then you just have to do appointment.company to get it.

What's the best way to make Rails 3 / Active Record associations for classes of a simple point of sale program?

I have 5 classes that are part of a point of sales program:
Customer, Technician, Order, Ticket, Service
Each Order has one customer and many tickets. Each Ticket has on technician and many services.
How should I make the associations so that I can do the following lookup:
customer.order.find_by_date(10/20/2010).service.technician.name
This is what I am planning:
class Customer < ActiveRecord::Base
has_many :orders
has_many :tickets, :through => :orders
has_many :technicians, :through => :tickets
end
class Technician < ActiveRecord::Base
belongs_to :ticket
belongs_to :service
end
class Service < ActiveRecord::Base
has_many :technicians
belongs_to :ticket
end
class Ticket < ActiveRecord::Base
has_many :services
has_many :technicians, :through => :services
end
class Order < ActiveRecord::Base
has_many :tickets
has_many :services, :through => tickets
has_many :technicians, :through => services
belongs_to :customer
end
The relationship between customer and order is one-to-many. And the relationship between order and service is also one-to-many. Therefore you cannot do:
customer.order.find_by_date('10/20/2010').service.technician.name
It should be:
customer.orders.find_by_date('10/20/2010').services.each do |service|
service.technician.name
end
It's a similar problem as in your other question.

Resources