Rails - model relationship suggestion - ruby-on-rails

Is it possible to define the following relationship:
A 'student' belongs to a 'group'
A 'group' has many 'courses' and many 'students'
A 'student' has many 'courses' through the 'group' it belongs to
I know how to do it with one more table(adding a student_course table which holds the id of the student and the course it belongs to and then saying that the a student has_many :courses, through: :student_course).
In other words, could it be implemented just by editing the following tables?
class Student
belongs_to :group
end
class Group
has_many :students
has_many :courses
end
class Course
belongs_to :group
end

Not sure if you can do it with a Rails class method, but you can just implement it manually.
class Student
belongs_to :group
def courses
group.courses
end
end
class Group
has_many :students
has_many :courses
end
class Course
belongs_to :group
end

Try below associaion
student.rb
belongs_to :group
has_many :courses, through: :group
group.rb
has many :courses
has many :students
course.rb
belongs_to :group
has_many :students, through: :group

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.

How can I set this association between two models?

I have table:
Groups
and table
Library
Now, I would like to make association, that I can set which Library records can Groups records see (basicaly a checkbox). So that If user belongs to a Group, he doesn't see certain Library records.
One suggestion before you start - don't use Group as an ActiveRecord class name as it will override certain functionality that already uses that term by convention. For now, I'll assume you are using Group.
If a Library can belong to only one Group, you need a simple has_many-belongs_to association setup:
class Group < ActiveRecord::Base
has_many :users
has_many :libraries
end
class Library < ActiveRecord::Base
belongs_to :group
end
class User < ActiveRecord::Base
belongs_to :group
has_many :libraries, :through => :group
end
If Library can belong to several Groups, you'll need a join table (and ideally a join model) to set this up:
class Group < ActiveRecord::Base
has_many :users
has_many :group_libraries
has_many :libraries, :through => :group_libraries
end
class GroupLibrary < ActiveRecord::Base
belongs_to :group
belongs_to :library
end
class Library < ActiveRecord::Base
has_many :group_libraries
has_many :groups, :through => :group_libraries
end
class User < ActiveRecord::Base
belongs_to :group
has_many :libraries, :through => :group
end

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

How to set up associations in 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

Help with Rails ActiveRecord managing queries

I have 3 models. Users, Groups, Employees all of the three have many to many.
user has many groups
groups have many users
groups have many employees
employees have many groups
So I've created two new models:
Departments (handles many to many between Users and Groups)
Employments (handles many to many between Groups and
Employees)
I believe I have this correct on paper but I can not get it down to code properly as I am new to rails. Because of this the data fetch does not seem to be correct.
This is what I have:
Employment:
class Employment < ActiveRecord::Base
belongs_to :group
belongs_to :employee
end
Department:
class Department < ActiveRecord::Base
belongs_to :group
belongs_to :user
end
User:
class User < ActiveRecord::Base
has_many :departments
has_many :groups, :through=>:departments
has_many :employees, :through=>:departments, :source => :group
end
Group:
class Group < ActiveRecord::Base
has_many :departments #new
has_many :users, :through => :departments #new
has_many :employments
has_many :employees, :through => :employments
end
Employee:
class Employee < ActiveRecord::Base
has_many :employments
has_many :groups, :through => :employments
end
I think biggest problem I have is to figure out how to get total employees for a user. In sql it would work with this query:
select * from employees where id in (select employee_id from employments where group_id in (select group_id from departments where user_id = 4))
This might work for you...
class User < ActiveRecord::Base
has_many :departments
has_many :groups, :through=>:departments **, :include => :employee**
has_many :employees, :through=>:departments, :source => :group
end
User.find(4).groups.collect { |c| c.employee.size }

Resources