I read "Multitenancy with Rails" by Ryan Bigg and I'm creating a multi-tenant application using Ruby on Rails.
I make two models, Tenant and User.
Tenant has many User, User belongs to Tenant.
To associate these models, I made this file,
active_record_extensions.rb
ActiveRecord::Base.class_eval do
def self.scoped_to_tenant
belongs_to :tenant
association_name = self.to_s.downcase.pluralize
Tenant.has_many association_name.to_sym, class_name: self.to_s
end
end
and add "scoped_to_tenant" to User.rb
class User < ActiveRecord::Base
scoped_to_tenant
end
When I want to get all users of one Tenant(id=1), I can get it by these code.
Tenant.find(1).users
The question is, what is the difference between I write
belongs_to :tenant
to User.rb and use scoped_to_tenant method ?
In both case, Tenant.rb is this.
Tenant.rb < ActiveRecord::Base
has_many :users
end
Thank you for answer.
I may get English wrong, so please tell me if you can't understand something.
A call to scoped_to_tenant method call the method belongs_to for you and add the many association to Tenant.
This is same as doing this :
# app/model/user.rb
class User < ActiveRecord::Base
belongs_to :tenant
end
# app/model/tenant.rb
class Tenant < ActiveRecord::Base
has_many :users
end
The benefit of the scoped_to_tenant is that you don't care about adding has_many relationship to Tenant model.
If you only have one model to associate with Tenant, you don't need this extension.
Related
I have a pretty standard User/Role setup going on (a user HABTM roles, a role HABTM users). I'm using CanCanCan for authorisation, and the role you have defines what you can do around the application. This part is all working fine, but now I want to be able to have users inherit roles as part of having a subscription to different memberships.
Here are the models concerned:
class User < ApplicationRecord
has_and_belongs_to_many :roles
has_one :membership_subscription
has_one :membership, through: :membership_subscription
end
class Role < ApplicationRecord
has_and_belongs_to_many :users
end
class MembershipSubscription < ApplicationRecord
belongs_to :user
belongs_to :membership
end
class Membership < ApplicationRecord
has_many :membership_subscriptions
has_many :users, through: :membership_subscriptions
end
I was thinking that I might be able to just add a has_many: roles association to the Membership, and then say that the user has_many roles through their subscription to the Membership, as well as the current HABTM association that allows roles to be assigned directly.
This way you can directly attach roles to users like you can now (as some roles are additive, and not related to the membership subscription/type at all) but also users will automatically inherit roles (and lose them again) as their membership subs come and go.
Does that make sense? I guess the other option would be to use callbacks in the model to deal with creating/deleting role associations but it doesn't seem as elegant.
Any advice greatly appreciated!
Okay so I think this is a valid answer:
First, update the models so that there is an associations between the Memberships and the Roles:
class Role < ApplicationRecord
has_and_belongs_to_many :users
has_and_belongs_to_many :memberships
end
class Membership < ApplicationRecord
has_many :membership_subscriptions
has_many :users, through: :membership_subscriptions
has_and_belongs_to_many :roles
end
Next, create a method in the user model that can be used to retrieve both directly assigned roles and inherited roles:
def combined_roles
if self.membership == nil
self.roles
else
self.roles + self.membership.roles
end
end
Then wherever you need to check a role, call that method instead of the usual user.roles
Not sure if that's a naive way of doing things, but seems to work okay. Comments still welcome if there's a better way
EDIT:
This allows a user to have the same role multiple times - it can be assigned directly or inherited. Modify the combined_roles method like so so that it strips out duplicates:
def combined_roles
if self.membership == nil
self.roles
else
(self.roles + self.membership.roles).uniq
end
end
I'm using rails devise gem for users authentification, but now for my shop i must introduce user groups for discont's, special prices, etc. How to do this with devise? Note that this is one/many-to-many, becouse every user can have many groups, and every group some users.
Also when user is registering i't group must be for example 1.
Devise has "closed" controller (not as in authlogic). That is trouble also.
def create
super
group = Group.find_by_name("newuser")
user_group = UserGroup.create
user_group.user_id = current_user.id
user_group.group_id = group.group_id
user_group.save
end
This doesn't necessarily have to be integrated with Devise unless I'm reading your question wrong. Just create a Group model describing the attributes of a group, and a UserGroup join model:
class UserGroup < ActiveRecord::Base
belongs_to :user
belongs_to :group
end
class User < ActiveRecord::Base
has_many :user_groups
has_many :groups, :through => :user_groups
# attr_accessible :user_id, :group_id
end
class Group < ActiveRecord::Base
has_many :user_groups
has_many :users, :through => :user_groups
end
As for the closed controller problem, you can lift the Devise controller into your application, or create a new controller which inherits from it and thus override the methods. Read more from their link GitHub page here.
Edit: I think you are approaching this from the wrong angle. You needn't do anything from within Devise's controllers, but rather add a before_save callback to your User model.
class User < ActiveRecord::Base
before_save(:on => :create) :assign_default_group
# Other model stuff here
private
def assign_default_group
# This automatically creates the UserGroup record
self.groups << Group.find_by_name("User")
end
end
Can someone help me to correct my associations ?
I have the following models:
User, Developer, Application, Comments, Rating, Permission
Requirements:
A user can be a Developer or not.
A user can have Default Permissions and Permissions for each application
A user can install multiple Applications
A user can comment and rate multiple Applications
A developer can develop multiple applications
An application can request a list of permissions.
I already created some associations but I believe its not 100% correct or an easier way to do it exist.
Can someone suggest me a correct way to do it?
You are confusing models with authorization.
You should check out CanCan for role based authorization. For example you don't need your developer model since its just a user with a different role/permissions.
Edit: Changed 'role based authentication' to 'role based authorization'. As the comment below points out the difference between authentication and authorization.
I think this is what you want as far as your model work. You can use a join model to manage your application permissions, and use Rails STI to manage what each type of user can do, whether it's developing or not.
user.rb
class User < ActiveRecord::Base
has_many :apps
has_many :comments, :through => :user_comments
has_many :ratings, :through => :user_ratings
end
comment.rb
class Comment < ActiveRecord::Base
belongs_to :user
end
rating.rb
class Rating < ActiveRecord::Base
belongs_to :user
end
user_comment.rb
class UserComment < ActiveRecord::Base
belongs_to :app
end
user_rating.rb
class UserRating < ActiveRecord::Base
belongs_to :app
end
normal_user.rb (STI)
class NormalUser < User
end
developer.rb (STI)
class Developer < User
end
app.rb
class App < ActiveRecord::Base
has_many :permissions, :through => :app_permissions
has_many :user_comments
has_many :user_ratings
end
permission.rb
class Permission < ActiveRecord::Base
belongs_to :app
end
app_permission.rb
class AppPermission < ActiveRecord::Base
end
I agree with #Mark, don't use STI. The better way will be implement as suggested by #Chris Barretto except for STI and use CanCan for role based authentication. The change for User model will be:
class User < ActiveRecord::Base
has_many :apps
has_many :comments, :through => :user_comments
has_many :ratings, :through => :user_ratings
has_many :roles
end
And there will be another model for Role:
class Role < ActiveRecord::Base
has_many :users
end
If you are using gems like Devise for authentication, it will be much easy.
I have what I'd think would not be a unique situation with ActiveRecord, but I can't seem to find anyone with a similar issue, so here goes:
I have a User class for users and a Roles class that defines the capabilities of the user. For ex, you could have a user with a role of 'tutor', a user with a role of 'student', and a user with a role of ['tutor', 'student']:
class User < ActiveRecord::Base
has_many :roles
end
class Role < ActiveRecord::Base
end
What I'd like to do is add activerecord associations based on roles. Clearly, a student may have many :courses, a tutor may have a :subject they teach, and both a student and tutor could have many :appointments, but it doesn't seem like adding all these associations to every User instance is the right way to go.
Subclassing User also seems wrong - I thought about doing Tutor < User and Student < User, and adding the proper associations in each subclass, but what if we have a student that is a tutor? Then we need a StudentTutor class? If more roles are added, this route seems dangerous.
I've considered doing something like:
class User < ActiveRecord::Base
protected
after_initialize do
if self.has_role?(Role::STUDENT)
has_many :courses # This does not work
else
# etc etc etc
end
end
end
But I have no idea if this is considered wrong or how I'd even make it work. What's the best method for dealing with this kind of user/role setup with associations?
I would recommend a gem for your issue. See a related SO question: Recommendation for Role Gem
I would do the following:
class User < ActiveRecord::Base
has_many :roles
end
class Role < ActiveRecord::Base
end
class Tutor < Role
has_one :subject
end
class Student < Role
has_many :courses
end
I've been reading up on a bit of STI Inheritence examples for Rails,
While it looks like it would suit my needs (User role/types inheriting from a base class of user). It seems like it doesn't handle a user having multiple roles too well. Particularly because it relies on a type field in the database.
Is there a quick and easy work around to this?
I'm not sure if STI is the right solution in a user role/user scenario - I don't think of User roles/types as a specific form of user. I would have the following schema:
class Membership < ActiveRecord::Base
belongs_to :user # foreign key - user_id
belongs_to :role # foreign key - role_id
end
class User < ActiveRecord::Base
has_many :memberships
has_many :roles, :through => :membership
end
class Role < ActiveRecord::Base
has_many :memberships
has_many :users, :through => :membership
end
Another way of doing this would be to use has_and_belongs_to_many. You might also want to check out the restful_authentication plugin.
A good workaround for your problem could be the easy_roles gem. You can find it on github.
As the others already said STI is not the way you can implement your stuff.
Just like Andy said, has_and_belongs_to_many is another way to do this.
# user.rb
class User < ActiveRecord::Base
has_and_belongs_to_many :roles
end
# role.rb
class Role < ActiveRecord::Base
has_and_belongs_to_many :users
end
Note: You still need the association table in your database but it's just hidden from your models.