There are 4 tables present: Course, Section, Enrollment, Student.
A Course has_many Sections
Sections and Students share a many_to_many relationship where Enrollment is the join table
I am trying to add a validation in Enrollment so that a student can't be enrolled into multiple Sections of the same Course. I can't figure out how to implement this validation because Course is not immediately related to Enrollment.
Here are the models for further clarification:
class Course < ActiveRecord::Base
has_many :sections
end
class Section < ActiveRecord::Base
belongs_to :course
has_many :enrollments
has_many :students, through :enrollments
end
class Enrollment < ActiveRecord::Base
validate :noDuplicateCourses
def noDuplicateCourses
if #TRYING TO FIGURE OUT HOW TO EXPRESS THE LOGIC HERE
errors.add(:student_id, 'Already enrolled in a different section of this course')
end
end
belongs_to :section
belongs_to :student
end
class Student < ActiveRecord::Base
has_many :enrollments
has_many :sections, through :enrollments
end
You can simply store the course_id on Enrollment. This method is called de-normalization, meaning that you have duplicate content which you try to avoid in relational databases.
Something like this should work. Just put your logic in place of your_logic_here.
validate :multiple_enrollment
private
# Custom validator
def multiple_enrollment
if your_logic_here
errors.add(self.class.table_name, "Student is already enrolled in another section of this course.")
end
end
Related
I am trying to wrap my head around how I am going to be able to have a has_many (or has_one in my case) association with a compound foreign_key.
For my example Assume this:
Imagine I am managing relationships between sellers and buyers, there is an initial contract between them and after that any amount of invoices. So the models look like this:
class Invoice
belongs_to :seller
belongs_to :buyer
end
class Seller
has_many :contracts
has_many :invoices
end
class Buyer
has_many :contracts
has_many :invoices
end
class Contract
belongs_to :seller
belongs_to :buyer
end
For every invoice there is one initial contract (defined by the seller and the buyer). I'm aware that there is all kinds of solutions to build a query that achieves that. Ideally though I want to take care of preloading/joining the contract for a list of invoices to avoid N+1 problems:
class Invoice
belongs_to :seller
belongs_to :buyer
has_one :contract # options go here
end
EDIT: The schema is what it is. Solutions that require a schema change unfortunately is not an option.
Just make the invoice belong to the contract and get the buyer and seller from that.
class Invoice
belongs_to :contract
end
Then get the invoices through contacts...
class Buyer
has_many :contracts
has_many :invoices, through: :contracts
end
class Seller
has_many :contracts
has_many :invoices, through: :contracts
end
So I have two tables, a Customers table and a Companies table. I have also created an empty Employees table that I would like to use as a join table.
These are the associations I have: (I want customers to be associated with their respective company)
class Company < ApplicationRecord
has_many :employees
has_many :customers, :through => :employees
end
class Customer < ApplicationRecord
belongs_to :employees
end
class Employee < ApplicationRecord
belongs_to :customer
belongs_to :company
end
Where would be the best way to do this? In my Customer#new method in the controller? I read that I need to use <<, but I don't know how to approach that.
You need to use the concept of Inverse association here:
class Customer has_many :companies, :through => :employees
You could just try delegating the company call on customers to their employee:
customer.rb
delegate :company, to: :employee
Now whenever you ask a customer its company, it will ask its employee to handle it.
I have Users and Trucks. I want the ability to say #truck.drivers << #users and #user.truck = #truck.
The solution is simple until I want the relationship to be stored in a join table.
# tables
:users
:id
:truck_drivers
:user_id
:truck_id
:truck
:id
I've gotten it to where I can say #truck.drivers << #user and #user.trucks << #truck, but I would like to limit a user to occupy one truck at a time, for my sanity.
Is it possible? A has_many/belongs_to with a join table? Or should I try a different approach? I'm not using a third model for the join table. It's just a table. Here's what I have so far.
class User < ApplicationRecord
has_and_belongs_to_many :trucks,
join_table: :truck_drivers, # points to the table
class_name: :Truck # looks for the truck model in the table
end
class Truck < ApplicationRecord
belongs_to :company
has_and_belongs_to_many :drivers,
join_table: :truck_drivers,
class_name: :User
end
The reason I need a join table in the first place is because each User can have many Roles: Admin, Manager, Driver, Customer Service, etc. I thought it didn't make sense to add a truck_id to all the users if all the users are not going to be using trucks.
It seems like you ought to be able to do something like:
#user.trucks << #truck unless #user.trucks.any?
Yes this is a standard strategy with rails using the :through keyword.
rails documentation: http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
Make a model called TruckUser with truck_id and user_id
then edit your classes:
class User < ApplicationRecord
has_many :truck_users
has_many :trucks, through: :truck_users
end
class Truck < ApplicationRecord
belongs_to :company
has_many :truck_users
has_many :drivers, through: :truck_users
end
class TruckUser < ApplicationRecord
belongs_to :truck
belongs_to :user
end
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
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