What I'm looking for is an appropriate way to set up a system where users can create projects and therefor become the admin of that project. The user can then add other admins to the project. Finally, other non-admin users can join the project.
I want to be able to verify whether a user is an admin of a project to check whether he has edit/update privileges. Any thoughts?
I figure I'll probably have a users_projects table and a projects_admins table, but I can't figure out how that translates to Rails relationships....
Thanks!
Ok, I will give it a try, but without too much code here in.
I see here 3 models:
User
Project
ProjectAdmin
The first 2 are simple models, with some attributes. The third one is the relation between the two and will be a n:m relation. So it is best to use the has-many-through relation here.
class ProjectAdmin < ActiveRecord::Base
belongs_to :project
belongs_to :user
end
class User < ActiveRecord::Base
has_many :project_admins
has_many :projects, :through => :project_admins
end
class Project < ActiveRecord::Base
has_many :project_admins
has_many :admins, :through => :project_admins
end
Of course you have to create additionally the 3 tables by migrations, and add later a similar relation for project users, named then ProjectUser as model. Have at least a look at the rails guide about relations, section "has-many :through".
To add the creator to a project, this should be a one-one relation between the two, so it should be sufficient to have:
class Project
has_one :creator, :class_name => "User"
end
(and of course the creator_id in the migration)
Related
I have two models User and Project which are in has_and_belongs_to_many association
The user model has a column status
i can access the status of a user who is in project like
project.user.status
but a user can be in different projects i want his status to be on project level instead of on his id
If I understand your question correctly, the problem is that you need to associate the status of the user to one of potentially many projects that the user is associated with, but you are associating a single status to a single user instead of the project.
In that event, you need to abstract this association to an additional model, for instance "UserProjectStatus" which would be associated with both the User and the Project. You can do this using the has_many, through association. This would end up with something along the lines of:
class Project < ApplicationRecord
has_many :user_project_statuses
has_many :users, through :user_project_statuses
end
class UserProjectStatus < ApplicationRecord
belongs_to :user
belongs_to :project
end
class User < ApplicationRecord
has_many :user_project_statuses
has_many :projects, through :user_project_statuses
end
There is a good overview of this any many other Rails ActiveModel associations at https://guides.rubyonrails.org/association_basics.html#the-has-one-through-association.
Hope this helps!
I have three models; User, Project, Skill. I have associated user & skill and skill & project. Now I want to assign a project to users based on skill, if they have the skill needed for the project they can be assigned, otherwise can't. No limit on how many users can work on one project.
I am working on rails. Need a start to the problem.
One method to get a view of available projects/users would be to add has_many :through to each using the skills table....
def User < ActiveRecord
belongs_to :skill
has_many :available_projects, through: :skill, source: :project
end
def Project < ActiveRecord
belongs_to :skill
has_many :available_users, through: :skill, source: :user
end
Then call the associations as needed...
user.available_projects
and...
project.available_users
I'm trying to set up models in such a way that Users can create Lessons and then other users can sign up for them.
Right now my models are set up like this:
class Lesson < ApplicationRecord
belongs_to :teacher, class_name: 'User'
has_many :students, class_name: 'User'
end
class User < ApplicationRecord
has_many :lessons
has_many :students, :through => :lessons
end
I want to be able to access the users signed up for a lesson by #lesson.students for example. I'd also like to be able to get all the lessons that a student is participating in (can't really see how I'd do this with my current set up).
Are my model associations right for how I'd like to use them? If so, how can I create the migrations to add the necessary references to my database models?
If you want the ability to create nested resources from it's parents then you have to add:
accepts_nested_attributes_for
to the parent model.
Also, I recommend you to read how to set up has_many through relationships, you need a join model for rails to do its magic and link the 2 models
Once you set everything up, create the join model (with it's respective foreign keys, one for lesson and the other for user) rails will take care of the associations between the models, allowing you to do things like:
User.last.lessons #lessons created by the last user
and
Lesson.first.users #users subscribed to a lesson, in this case the first one
My group is making a Project Management System for our course, and it's my job that when a user is logged in, that they see the projects they are part of, and also that the projects listed have a members list of current members of that project.
(Also will need an add/delete member function later)
My question is, since the rest of the group have already set it up so that Projects belong to Users, is it possible to have Users belong to Projects in order to set up this member list and do what I'm talking about?
The relation you are describing is not one-to-one:
when a user is logged in, that they see the projects they are part of
This implies that a user can have several projects. You also specified:
[project has a] list of current members of that project
This implies that a projet can have several users.
In conclusion, you need a many-to-many relation between your User and Project models.
This is a basic many-to-many relationship in Rails:
class User < ActiveRecord::Base
has_many :user_projects
has_many :projects, through: :user_projects
class Project < ActiveRecord::Base
has_many :user_projects
has_many :users, through: :user_projects
class UserProject < ActiveRecord::Base
belongs_to :user
belongs_to :project
validates :user_id, :project_id, presence: true
The UserProject model is a join table. What I have done in my code is an explicit has_and_belongs_to_many, which let you have more control over the join table. (example: add a role column in the UserProject table, containing data like project_creator or simple_member)
The UserProject model could be named Membership to be more explicit. I used both models' name to make UserProject, as we usually do in Rails' naming convention.
I am currently using has_and_belongs_to_many to implement a many-to-many relationship. I would however want to put in a attribute in the many_to_many table.
Basically I am creating a email system. I have users and conversations. A user can have many conversations and a conversations can also have many users. However, I am trying to make it so that I can have a read/unread attribute to show which messages are read. Since conversations can have many users, it is not practicable to put the attribute in the conversations table as then it would mean that the conversation is read by all. So I think it would work best in the middle table. I am wondering though how I can access that attribute in the middle table. If the attribute is read. What code do I put in to access that and how do I update the attribute. As mention above I am using has_and_belongs_to_many
If you want to have additional attributes to your has-and-belongs-to-many association, you have to build a model class for that relation. See the detailed description in the Rails Guides about it.
After having read it for myself, this is now deprecated with the current version of Rails, so you really should switch to has_many :through. Your models could be (copied and changed from the Rails Guides, I don't know if connection is a good name for the m2n relation):
class User < ActiveRecord::Base
has_many :connections
has_many :conversations, :through => :connections
end
class Connection < ActiveRecord::Base
belongs_to :user
belongs_to :conversation
end
class Conversation < ActiveRecord::Base
has_many :connections
has_many :users, :through => :connections
end
There you are able to add additional attributes to your connections table, and refer in the code to them.