Rails 5 - belongs_to but has_many association - ruby-on-rails

I am currently trying to create a system which allows for specific users to create a Course record which can be enrolled in by many other users. I've tried a few association techniques such as has_and_belong_to_many, has_many :through and number of other setups but have been unable to get it right.
Basically all that I need is the following:
Course belongs to (is created by) a single User (foreign_id => admin_id)
Course has many enrolled Users (Join Table?)
User has many created Courses
User can belong to many Courses
If you have any idea how this would be accomplished I would greatly appreciate your input.

I would use a Course model to represent the course information, with a user_id attribute to associate with the user who created the course. I would also make an association table/model called Enrollment which is an association between a User and a Course. You can then do something like this:
#User.rb
has_many :courses
has_many :enrollments
has_many :enrolled_courses, through: :enrollments, source: :course
#Enrollment.rb
belongs_to :course
belongs_to :user
#Course.rb
belongs_to :user
has_many :enrollments
has_many :users, through: :enrollment
With this configuration you can call course.user to receive the user who created the course, but you could also call course.users to receive the users who are enrolled in the course. On the opposite side, you can call user.enrolled_courses to receive the list of courses a user is enrolled in, or user.courses to receive a list of courses a user has created.

Related

How to make a resource that belongs_to multiple users in Rails

I'm setting up an app that has Users and Brands. Most users will not be associated with a brand, and will only be able to comment on Brand pages. Some users, however, will be associated with a single brand. I want these users to be the "admins" or owners of this brand. E.g. Users A and B are both "admins" of a brand, and so can create/edit/update the brand, etc. My question is, how should I set up the Brand resource such that it "belongs_to" multiple users? I understand that I could say brands have_many users, but is it weird to say that an object "has" a user? Is it more appropriate to "belong" to users? This description leads me to believe so: "For example, it makes more sense to say that a supplier owns an account than that an account owns a supplier."
It's definitely a has_many relationship.
But it may be clearer to refer to those special users as 'administrators' or 'admins'
class Brand
has_many :administrators, class_name: 'User'
end
If it turns out that a user can be administrator for several brands, then you'll need a join table, either HABTM or HMT. HMT is the better choice in case you want to store characteristics about the join (e.g. when he became administrator)
class BrandUser
belongs_to :user
belongs_to :brand
end
class Brand
has_many :brand_users
has_many :administrators, through: :brand_users, source: :user
end
class User
has_many :brand_users
has_many :brands, through: :brand_users
end

ActiveRecord relationship model with Users and Organizations

I'm trying to find out what's the best logical way to model relationship between models.
I have 4 models:
User
Product
SlackTeam
Organization
Here User has many Products, SlackTeams and Organizations, and SlackTeam belongs to User and has one Organization. Organization should belong to User and SlackTeam. Am I logically correct here?
The workflow is following:
Users can log in with SlackTeam (which automatically creates Organization)
other Users from the same slack team will be added to same Organization once they link up their account with Slack
if Users are connected to many SlackTeams (and Organizations) they can filter to see Products from all Organizations they are part of or only from one
Am I missing something?
class User
has_many :users_ogranizations
has_many :organizations, through: :users_organizations
has_many :products, through: :organizations
end
class Product
belongs_to :organization
end
class Organization
has_many :users_ogranizations
has_many :users, through: :users_organizations
has_many :products
end
class UsersOrganization
belongs_to :user
belongs_to :organization
end
# i'd rather use slack profile than team, because Organization and Team
# already connotes the same thing.
class SlackProfile
end
You can handle your user's login however you like, I would prefer a kind of authentication service, though. All products that belongs to the organization is now accessible to the user, you can then filter the products with:
current_user.products.where(organization: Organization.last)

Planning a Rails application associations

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

User and Team modeling

I have been doing some searching around Google and this site for some ways to go about constructing some models for an application I am working on.
I plan to have Users and Teams. A user can join a team and one team only. A team can obviously have many users. Also, I want to add a 'leader' role inside the team that is assigned to the User who creates a Team but can later be passed on to another User who is already a part of that team.
I have seen some examples of models for something like this and saw a few like this:
class User < ActiveRecord::Base
has_one :team
has_many :memberships
end
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :team
end
class Team < ActiveRecord::Base
has_many :memberships
has_many :users, :through => :memberships
end
Why is the Membership model necessary? I have seen it in a few examples and I am not sure I quite follow the logic of why it is in there and what it is doing.
Also, if I did something like has_one :leader in the Team model and had the Leader model, would that be considered the best practice for determining a leader in a Team? It seems like a bit much to have an entire model/table for Leader.
The Memberships model is allowing for a many-to-many relationship there. It's acting as the join table. It would allow Users to belong to many Teams and Teams to have many Users.
Sounds like you just want a one-to-many though?
class User < ActiveRecord::Base
belongs_to :team
end
class Team < ActiveRecord::Base
has_many :users
end
I'll have to double check this part but you can use additional parameters to specify the models in your assocation if they don't match up with the name you need. So Team could have a leader that's just a User model.
class Team < ActiveRecord::Base
has_many :users
has_one :leader, :class_name => "User", :foreign_key => "user_id"
end
the rails guides page on associations has a good summary including the part about the :class_name and other options
http://guides.rubyonrails.org/association_basics.html#the-has_many-association

Rails User Membership Groups

I'm trying to make a application where a user belongs to multiple courses and multiple assignments belong to a course. I'm using devise for the user model. I want to be able to find all the courses a user belongs to and all the assignments their courses have.
User model:
has_and_belongs_to_many :course
has_many :assignments, :through => :courses
Course model:
has_and_belongs_to :user
has_many :assignments
Assignment model:
belongs_to :course
this requires an intermediate table CoursesUsers with columns user_id and course_id
and column course_id in Assignment
with this give you can do things like
current_user.courses
current_user.assignments
some_course.assignments
some_course.users
(assuming there is a current_user or some_course)
Read about details here: Active Record Associations Especially how to setup the has_and_belongs_to_many association

Resources