User acts as Student and Teacher in my model associations - ruby-on-rails

I have a Student, Teacher, and University model. A Teacher has_many Universities and a Student has_one University.
Currently there is no Teacher or Student model but a single model User. In this way what would be relation between User and University? And what would be the correct schema?

Firstly,you are lot confused with the associations.It is university has_many teachers not
Teacher has_many Universities and also it is not Student has_one University,it is student belongs_to university.
And your point of giving only one model as User instead of having two models(teacher and Student) should be given like this
class University < ActiveRecord::Base
has_many :teachers, :class_name => "User"
has_many :students, :class_name => "User"
end
class User < ActiveRecord::Base
belongs_to :university
end
And I would recommend to read these Guides before going any further with associations.
Hope it helps!
Update
Well,in that point,you can do like this
class University < ActiveRecord::Base
belongs_to :teacher, :class_name => "User",:foreign_key => 'user_id'
has_many :students, :class_name => "User"
end
class User < ActiveRecord::Base
has_many :universities
end

I believe you are looking for something called single table inheritance. Check out this article:
http://www.alexreisner.com/code/single-table-inheritance-in-rails

Related

How to associate through a join table that reference the same model?

I have a model called House and I want to be able to associate houses with each other to show recommendations.
So I would expect that given a house, I should be able to ask: house.recommended_houses. A house could be recommended for more than one house.
I was thinking on having a table that would store this association (I don't know the name yet), so it would have the following columns:
recommended_house_id
recommended_for_house_id
I am failing to understand how would I hook this up with my House model. What would the associations look like, and also what name should I be using for that join model?
This should get you started:
class House < ApplicationRecord
has_and_belongs_to_many :recommendations,
class_name: "House",
foreign_key: "recommended_by_id",
association_foreign_key: "recommendation_id"
end
What you're describing is called a self-referential association.
You can set up a join table (recommendations) and the associated model:
class Recommendation < ActiveRecord::Base
belongs_to :house
belongs_to :recommended_house, :class_name => 'House'
end
and then use has_many, :through relationships within the House model to set up the relationships you're looking for.
class House < ActiveRecord::Base
has_many :recommendations
has_many :recommended_houses, :through => :recommendations
has_many :inverse_recommendations, :class_name => "Recommendation", :foreign_key => "recommended_house_id"
has_many :recommended_by_houses, :through => :inverse_recommendations, :source => :house
end
Now you can use both house.recommended_houses and house.recommended_by_houses.

Rails associations, has_one to has_many

In my app I have 2 classes. User and Classroom. I use the user class as a student as well.
I'm trying to achieve a result where:
A classroom belongs to a user.
A user has many classrooms.
A classroom has one student through the user class.
A student can be associated to many classrooms.
To try and explain further. I have a classroom and the user is the creator of the classroom. When someone joins they are a student of the classroom and I only want there to be one student and one creator.
I want a student to be attached to lots of different classrooms and I want the classrooms to all belong to one user.
My current code for the two classes looks like this:
class User < ActiveRecord::Base
has_many :classrooms
end
class Classroom < ActiveRecord::Base
belongs_to :user
has_one :student, :class_name => "User"
end
Any advice is much appreciated. Thanks!
I think what you are trying to achieve is:
class User < ActiveRecord::Base
has_many :classroom_users
has_many :classrooms, through: :classroom_users
end
class ClassroomUser < ActiveRecord::Base
belongs_to :classroom
belongs_to :user
end
class Classroom < ActiveRecord::Base
has_many :classroom_users
has_many :users, through: :classroom_users
end

Rails belongs_to and has_many foreign_key relationship

I need a little help... I have these relationships... Users belong to Department, a Department has a manager, Managers (Users) can have many managed departments.
I'm having one of those days and I can't for the life of me figure out what to put inside the User model to define the `has_many :managed_departments' part of the relationship.
Department
class Department < ActiveRecord::Base
has_many :users
belongs_to :manager, foreign_key: "manager_id", class_name: "User"
end
User
class User < ActiveRecord::Base
belongs_to :department
# has_many :managed_departments
end
This works: Department.last.manager which returns:
=> #<User id: 2, etc...
I'm having a mindblank on what to put in the User model.
Can anyone help?
You can use class_name option same like you used it in Department model
#user.rb
class User < ActiveRecord::Base
belongs_to :department
has_many :managed_departments, class_name: "Department", foreign_key: "manager_id"
end
You are not creating right association. You have many to many relation ship between user and department.
user has_many departments (Can manage multiple department)
department has_many users
As a database standard you should break many to many relationship and introduce a new intermediate table.
So your new table should be users_departments. In this table you can add column user is manager or not.
table should have column :
user_id , department_is, is_manager
class Department < ActiveRecord::Base
has_many :users, :through => :users_departments
end
class User < ActiveRecord::Base
has_many :departments, :through => :users_departments
end
class UsersDepartment < ActiveRecord::Base
belongs_to :user
belongs_to :department
end
Here you can find anything with association. and with simple scope you can find manager of department also.

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 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

Resources