I need following association in my app.
User has many role,
Project has many user,
user1: manager for project 1 & employee for project 2
I am not getting idea about how can I set relationship between these tables and models.
User, Role and Project
How can I set this up?
Thanks a lot in advice.
All you need to do is use some gems.
Devise
Rolify
Then create the project model + controller + views.
create a new user - User.create(email: "user1#example.com", password: "123123")
add role to the newly created user - User.last.add_role(:manager)
create another user - User.create(email: "user2#example.com", password: "123123")
add role to the newly created user - User.last.add_role(:employee)
Now, if you do all the steps right, then create a new Project:
user1_project = User.find(1).projects.new(project_params)
It will create a project for user with email user1#eaxmple.com who has manager role.
Well you can do something like this,
Create a table call role and give unique id to each role_type, like
1 for manager, 2 for employee, 3 for owner etc.
And create another table which have the following fields,
user_id,role_id,project_id
So if user1 is manager of project1 then we will have a entry with the following entry
user_id = 1, role_id = 1, project_id = 1
and user1 is employee for project2 will have an entry,
user_id = 1, role_id = 2, project_id = 2
here role_id 1 and 2 corresponds to manager and employee respectively.
You could achieve this setting up Rolify gem.
class User < ActiveRecord::Base
rolify
end
class Project < ActiveRecord::Base
resourcify
end
Now you can add multiple roles to user object either by specifying particular project object or Project class or without these. For example:
user = User.create(user_params)
project_1 = Project.create(project_params)
project_1 = Project.create(project_params)
# Add roles with respect to specific project object
user.add_role('manager', project_1)
user.add_role('employee', project_2)
Please go through this rolify documentation to get the detailed overview and usage examples.
if you asking about how to add the relationship to the tables.
Here is an example
Let's say
Users -> user1, user2
Roles -> Role1, Role2
Projects -> P1, P2
Users -> Roles ( foreign key relationship ) users depends on Roles
Roles -> Projects ( foreign key relationship ) Roles depends on Projects
Here let's treat project as resource where a role can access them
Users -> Roles -> Projects
Roles can have read permission on P1 and read-write permission on P2 etc
Related
In the models :
Organization has_many Users
Users has_many Registrations
Registration belongs_to Organization
In my view organization#show, I am going to have something like
organization.users.each do |user|
user.registrations.where(organization_id: organization.id).each do |registration|
# display a registration
end
end
In the controller to avoid N + 1 query on registrations, I could do something like:
#organization = Organization.includes(users: :registrations)find(params[:id])
However this won't work because I actually have an extra criteria on registration where(organization_id: organization.id).
How can I preload all registrations that belong to a user within the organization and that belong to the said organization to avoid N + 1 query here.
Considering that a user belongs to an organization, there should be an organization_id in the users table, and the models must be related to each other. Perhaps you can try joining Registration with User and filtering by the users.organization_id column;
Registration.joins(:user).where(users: { organization_id: 1 })
I'm using Groupify gem to create separate roles in groups such as "
manager" "accountint" etc. In documentation is clear how to add object to groups with certaing membership: group.add(user, as: 'manager'). But there's no explanation how to check user memberships in certain group. I want to create admin panel for changing user roles and groups so it's very important.
Edit
I solved my problem
memberships = user.group_memberships_as_member.where("group_id = ?", group.id)
memberships.each do |membership|
if membership.membership_type.present?
puts membership.membership_type
end
end
You can use user.in_named_group?(:admin) (e.g. to check if admin is in the group.)
You can also do this:
User.in_any_group(group1, group2) # Find users that belong to any of these groups
I am using active admin. I have project and user model with many to many relationship between them. In my user model i have a project_leader boolean column. And in my project model i have project_leader as integer column. I am allowed to select 1 project leader for each project. And then id of the user who is the project leader is stored in Project project_leader column. How do i map the id of the user to its name from active admin index?
Solved:
In my project model i defined a method as following.
def get_associated_user
p = self.project_leader
user = User.find(p).full_name
end
And from the active_admin index i just called the method object.get_associated_user
You can do it lik this too
column "Project Leader" do |p|
user = User.find(p.project_leader).full_name
end
I'm trying to figure out how to set up the relationship/association for a Document model for my rails project. I have a User model and Users can friend each another (friendship model).
Now I want users to be able to invite (give access to) CERTAIN friends to modify and edit these documents similar to Google Docs.
This is my current approach to this problem:
Create a new relationship model called "group", essentially a subset of friends. Document belongs to a user and document belongs to a group. Users can then invite their friends into these group relationships and documents can be accessed/modified through these groups.
Therefore, User has many groups and group belongs to many users.
My question:
Is there a better approach to this problem?
You might consider a "Membership" join table. So:
Group >- Membership -< User
...since you requirement is some users will have abilities others wont (editing). There are all sorts of options from there, for example STI which would give you:
class Membership < ActiveRecord::Base
...general membership functions
end
class Editor < Membership
...editor specific code...
end
class Reviewer < Membership
...reviewer specific code...
end
And a controller class something like
class MembershipController
def create invitation
if invitation.for_editor?
# assuming invitation has one group, and group_memberships method that returns group.memberships
invitation.group_memberships.create! user: current_user
else
invitation.group_memberships.create! user: current_user
end
end
end
As an example, depending on your opinion of STI.
https://github.com/scambra/devise_invitable
might have more ideas.
Using Devise.
Models:
User belongs_to Organization
Organization has_many Users
During signup, I want to create the user's parent organization as well. So two pieces to the form: 1) organization info, and 2) basic user info (email/password)
I've done a bunch of searching for Devise and nested resources, but they usually talk about the model relationship being the other direction (User has_many).
Any ideas?
Thanks in advance!
Do you already know the organisations that the user could belong to?
In which case just have a drop down when they register and insert the ID of the organisation on save.
Otherwise, what will probably happen is that you basically end up with a 1:1 with organisation anyway given typos and so on, unless you are guessing based on the name they input. Does the organisation have any kind of security associated with it? If this is a public site it seems a little dangerous because people could camp in places they're not supposed to be.
That said:
o = Organisation.find_or_create_by_name(params[:org_name])
u = o.user.build(params[:user])
if u.save ... # etc.
or something like that.
u = User.new(params[:user])
if u.valid?
o = Organisation.create(params[:org_name])
u.organization = o
u.save
end