I have public and private projects in my app. I want to assign user to private projects for viewing and posting. What's the right way to do this? I tried with a permissionlist model and associated it to a project. but i got so confused that i couldn't make it.
What you need is a has_many :through relationship
Create a table
permissions
containing
user_id, project_id and permission
.
your models
class Permission < ActiveRecord::Base
belongs_to :user
belongs_to :project
end
class User < ActiveRecord::Base
has_many :permissions, :dependent => true
has_many :projects, :through => :permissions
end
class Project < ActiveRecord::Base
has_many :permissions, :dependent => true
has_many :users, :through => :permissions
end
in the permissions link the project, user and the permission the user has on that project.
I hope this helps.
Regards
Related
I have a memberships resource and it belongs to user and club. I want to access the parent attributes i.e for club and user and I read that accepts_nested_attributes_for is used for parent side of a relationship. What should I write in my membership model?
I have searched about it both in stackoverflow and activeadmin docs but I did not get a thorough explanation about solving my problem...
My membership model is:
membership.rb
class Membership < ApplicationRecord
require 'csv'
belongs_to :club
belongs_to :user
end
Also what should i write in my membership resource which I have already registered with AA...
You can mention the following :-
1) has_many: memberships #in user model
2) has_many: memberships #in club model
This will help you access parent attributes from child model :-
membership.user, membership.club
Also, you can mention accepts_nested_attributes_for: memberships in user model.
When you write this, you can then build a common form for user and membership and modify both of them simultaneously. To achieve this, you will have to allow membership attributes in users_controller.rb.
The following should work(Similar question):
class Club < ApplicationRecord
has_many :memberships, :dependent => :destroy
has_many :users, :through => :memberships
accepts_nested_attributes_for :membership
end
class User < ApplicationRecord
has_many :memberships, :dependent => :destroy
has_many :clubs, :through => :memberships
accepts_nested_attributes_for :membership
end
class Membership < ApplicationRecord
require 'csv'
belongs_to :club
belongs_to :user
accepts_nested_attributes_for :club
end
In Rails when I have made one Model as the foreign key in another model then I can delete that model while speciying its relation like:
class User < ApplicationRecord
has_many :garments, dependent: :destroy
end
But if I have one model which is created in another namespace like superadmin them how to write the dependent destroy relation in that case
for example I am using :
class User < ApplicationRecord
has_one superadmin::company , dependent: :destroy
end
which is incorrect.
The model company is present in namespace superadmin, please tell if their is a correct a way possible. Thanks in advance
It's incorrect, Way of reference to model and namespace with class name is incorrect:
incorrrect:
class User < ApplicationRecord
has_one superadmin::company , dependent: :destroy
end
corrrect:
class User < ApplicationRecord
has_one :company, :class_name => "Superadmin::Company", :dependent => :destroy
end
class User < ApplicationRecord
has_one :company, :class_name => "Superadmin::Company", :dependent => :destroy
#has_many :companies, :class_name => "Superadmin::Company", :dependent => :destroy
end
sorry for the title I didn't really know what to put in there to be really clear. So I'll be clear in my explanation.
Users can create Groups and Links via form. The users can join Groups via Member where member has group_id and user_id in the table. I would like to be able to share the Users Link within the group.
So when a user creates a group, other users join this group. And for now when a user creates a link, it's only for himself but I want the user to be able to share (or not) the links he created with the groups he is part of. If the user is a member of several groups then the user can choose in which group he wants to share the link he created.
I have 4 models : Link.rb, User.rb, Member.rb and Group.rb. Here are my relations :
#Link.rb:
class Link < ApplicationRecord
belongs_to :user
end
#User.rb :
class User < ApplicationRecord
has_many :links, dependent: :destroy
has_many :members, :dependent => :destroy
has_many :groups, :through => :members
end
#Member.rb :
class Member < ApplicationRecord
belongs_to :user
belongs_to :group
validates :user_id, :presence => true
validates :group_id, :presence => true
validates :user_id, :uniqueness => {:scope => [:user_id, :group_id]}
end
#Group.rb :
class Group < ApplicationRecord
has_secure_token :auth_token
has_many :members, :dependent => :destroy
has_many :users, through: :members, source: :user
belongs_to :owner, class_name: "User"
def to_param
auth_token
end
end
What I tried :
I added a reference of group_id in the link table. And added belong_to :group in link.rb, and has_many :links, dependent: :destroy in group.rb.
In my new link form I added the current_user group_id in a select (to retrieve only the groups where the user is in) and on create the link is created with the group_id and the current_user id. That works.
Problem is that I have to enter a group_id which means I give no choice to the user and he has to give a group_id so basically he must share the link to the group. Which is not what I want.
What I thought about :
Maybe I should just go for the same idea as I did for members. Which means having a new table like grouplinks where I give the group_id, link_id and the user_id with relations in place so I can use the grouplink.id to share in my group or not. Is this a good option ? If yes what are the relations I should put in place ? Any other suggestion, maybe I'm completly wrong and there is something easy to do and I don't see it.
I will try to give a shot with some code I thought about :
#Link.rb:
class Link < ApplicationRecord
belongs_to :user
belongs_to :grouplink
end
#User.rb :
class User < ApplicationRecord
has_many :links, dependent: :destroy
has_many :grouplinks, dependent: :destroy
has_many :members, :dependent => :destroy
has_many :groups, :through => :members
end
#Grouplink.rb :
class Member < ApplicationRecord
belongs_to :user
belongs_to :group
belongs_to :link
end
#Group.rb :
class Group < ApplicationRecord
has_secure_token :auth_token
has_many :members, :dependent => :destroy
has_many :users, through: :members, source: :user
belongs_to :owner, class_name: "User"
has_many :groupslinks, :dependent => :destroy
has_many :links, through: :grouplinks
def to_param
auth_token
end
end
Could that work out ?
Thanks for your help.
It's better to create a join table with group_id and link_id. Since the Group Admin is the user who created the group, you don't even need to add user_id(member_id I suppose) since every member of that group can access the link. Get the user groups and add that link to that particular group and check the association.
class GroupLink
belongs_to :group
belongs_to :link
You can just do
#group = #user.groups.find(params[:group_id])
#group_link = #group.group_links.build(link_id: link_id)
#group_link.save
Let me know if you need anything else or clarify this.
Im working on a Rails app where users can create projects. There are two types of users Admins and Collaborators. Both Admins and Collaborators has_many :accounts, through: :account_users, where account_users is a join table. When Admins deletes their accounts, I also want to delete their created account and it's project, but I can't get this to work. My models currently looks like this:
class Collaborator < User
[...]
has_many :account_users
has_many :accounts, through: :account_users
[...]
end
class Admin < User
has_many :account_users
has_many :accounts, through: :account_users, :dependent => :destroy
[...]
end
class Account < ActiveRecord::Base
[...]
belongs_to :admin
has_many :account_users
has_many :collaborators, through: :account_users
[...]
end
class AccountUser < ActiveRecord::Base
belongs_to :admin
belongs_to :account
belongs_to :collaborator
end
When a Admin users deletes it's account, only the row in the join table and user table is deleted, their projects isn't deleted.
Note, I use devise for handling authentication.
How could I solve this?
I don't see a project association, so I'm thinking you could do it in one of two ways:
class Account < ActiveRecord::Base
after_save :destroy_projects
private
def destroy_projects
self.projects.delete_all if self.destroyed?
end
end
or
class Account < ActiveRecord::Base
[...]
belongs_to :admin
has_many :account_users
has_many :collaborators, through: :account_users
has_many :projects, :dependent => :destroy
[...]
end
Am planning on using declarative authorization in a Rails 3 app.
I have the following model relationships:
class Role < ActiveRecord::Base
has_many :permissions, :dependent => :destroy
has_many :users, :through => :permissions, :uniq => true
end
class Permission < ActiveRecord::Base
belongs_to :user
belongs_to :role
belongs_to :context
end
class User < ActiveRecord::Base
has_many :permissions, :dependent => :destroy
has_many :roles, :through => :permissions
roles.map do |role|
role.name.underscore.to_sym
end
end
class Context < ActiveRecord::Base
has_many :contexts, :dependent => :destroy
end
The concept here is that I am going to segment various datasets into different contexts. However, a given user may have different roles for each context -- maybe an admin in one context and a basic user in another. I have implemented current_user and current_context for use in controllers and views.
I plan on using the if_attribute to reference the right permission on the right dataset.
However, the question is how do I make the def role_symbols only return the roles associated with the user in a particular context when I cannot/should not reference current_context in a model (where role_symbols is defined).
Any ideas?
Was luck enough to find the answer here:
http://blog.drivingthevortex.nl/2010/01/24/using-declarative_authorization-with-subdomains/