Rails associations, has_one to has_many - ruby-on-rails

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

Related

How to reference a field in the join model of a has_many though Rails 5 relationship

I am working on an app where users have many quizzes and quizzes can have many users. I have set the relationships:
class User < ApplicationRecord
has_many :studies
has_many :quizzes, through: :studies
end
class Quiz < ApplicationRecord
has_many :studies
has_many :users, through: :studies
end
class Study < ApplicationRecord
belongs_to :user
belongs_to :quiz
end
I have a field in the Study table to store the score that the user made on the quiz, but I am unable to access the field. I have tried #quiz.studies.score and #quiz.study.score but Rails give me an undefined method. How to I access the field in a join model of a has_many though relationship?
#quiz.studies return the collection of studies. So you have to use first, last, each to get the score of the specific studies.
Try this:
#quiz.studies.first.score

Rails type of relation between models

I have 3 models: User, Order and Car and I have question because I don't know what relationships between these models will be the best. Only requirement is that only one car per user in order.
A user can have many orders, and therefore, many cars through those orders.
class User < ActiveRecord::Base
has_many :orders
has_many :cars, through: :orders
end
An order belongs to a user and a car.
class Order < ActiveRecord::Base
belongs_to :user
belongs_to :car
end
A car has one order.
class Car < ActiveRecord::Base
has_one :order
end
So you need one on one relationship between order and car and then back to order and customer one relationship. Something below should do the trick.
class Car < ActiveRecord::Base
has_one :order
has_one :customer, through: :order
end
class Order < ActiveRecord::Base
belongs_to :car
belongs_to :customer
end
class Customer < ActiveRecord::Base
has_one :order
has_one :car , through: :order
end
But i will suggest the has_many relationship by the looks of the model name, but you know better your problem than me.

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.

User acts as Student and Teacher in my model associations

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

Ruby on Rails 3.1: Am I setting this relationship up correctly?

I am making my first app in Ruby on Rails 3.1....Do I have these relationships setup correctly? Essentially, a student/client will be able to login and rate a teacher. A client can have many teachers and a teacher can have many clients. Each client can create a rating for a particular teacher (a teacher can't rate clients). Ratings are optional.
I intend to be able to display a teacher's ratings from various clients and also allow clients to login and rate all the teachers they've had.
class Client < ActiveRecord::Base
has_many :ratings
has_and_belongs_to_many :teachers
end
class Teacher < ActiveRecord::Base
has_many :ratings
has_and_belongs_to_many :clients
end
class Rating < ActiveRecord::Base
belongs_to :teacher
belongs_to :client
end
I'd say that the usage of has_and_belongs_to_many should be used when you only have a database table and not a Rails model to join the models. In your case, since you do have a model called Rating then I'd say it is better to use has_many, :through.
To accomplish that, change your Teacher and Client models to look like this:
class Client < ActiveRecord::Base
has_many :ratings
has_many :teachers, :through => :ratings
end
class Teacher < ActiveRecord::Base
has_many :ratings
has_many :clients, :through => :ratings
end
The Rating model does not need any changing.

Resources