Multiple Devise Models or Single User Model - devise

I have a rails 4.1 application setup with Devise.
I have:
one USER model and one PROFILE model. The user has_one profile
However, I have two types of users - Buyers and Sellers (it's a service platform and I have to use states to check if the user & profile is 100% completed to mark a user active).
Everything is working up to this level, but I am looking for some suggestion on how to model these two user types. Please note that the sellers have more responsibility, like adding portfolio items, applying for jobs etc. The buyers on other hands only post jobs and message sellers. Think of it like any other standard marketplace.
My question is:
Whether to use STI, please if someone can let me know how to do this, using the current setup?
Use roles system using Cancan or other?
The current setup is:
User model:
class User < ActiveRecord::Base
has_one :profile, dependent: :destroy, autosave: true
class Profile < ActiveRecord::Base
belongs_to :user
All profile data of both users types is in profiles table.
Thank you for your time.

Related

Center <-> User model relation

My site will have many centers. The administrator of each center should being able to login and edit the center AND being able to create more users that have access to that center edition.
The first thing I thought of is to create 1-N Center-User relation, but not sure if is the right one. Looking to some other posts I realized that maybe using a 1-1 relation might not be a bad idea.
What would be the best solution model wise?
Thanks in advance.
Will there be more than 1 admin for a center?
What about a Users -> Roles -> Center
User has_many roles, Role belongs_to a center
This way you could have a user who is a role 'admin' for two different centers and the same user could be a role 'normal user' at another center. This is in my mind most flexible, not sure if its more flexible than what you need.
Given your comment of one center to user you can do something like this.
A User can have one Center and create more (so 1+) and can add more users to it.
class User < ActiveRecord::Base
belongs_to :center
#...
end
class Center < ActiveRecord::Base
has_many :users
#...
end
Then you can do things like
current_user.center
current_user.create_center(params)
some_center.users << user_to_add

Rails 3: Project-based permissions

I am building an internal project management software (ala Basecamp) that will be for our internal and external use. I am struggling to find the best strategy for doing advanced permissions though.
Like Basecamp, there will be a number of clients, with a number of projects under them. I want to be able to assign each user different viewing rights to each client and project, which will dictate who can see what. Basically, an admin should be able to say "this user can see these 4 projects", and then when that user logs in, they are presented with those 4.
I am using Cancan for overall permissions, but this does not appear to be able to assign in the way I need. I've looked at role_model, cantango, et. but can't find a use case that fits exactly what I am trying to do, even though it seems quite standard.
Any insight here?
Thanks!
Edit:
It has obviously occurred to me to just do a permissions table (described here: How to create a basic User Permissions per project association?). I struggle with a) is this the best way? and more importantly b) what is the best way to check permissions in the app and only show allowed data?
How about creating extra model Permission, that will hold permission for user for each project?
Of course you can use can_can, but this approach will make is simple to analyze and solution will be dedicated for problem.
ie.
class Permission < ActiveRecord::Base
TYPES = {
:user=>1,
:manager=>10,
:admin=>100
}
belongs_to :user
belongs_to :project
validates :permission_type,
:inclusion=>{:in=>Permission::TYPES.values},
:presence=>true
end
class User < ActiveRecord::Base
has_many :permissions
has_many :projects, :through=>:permissions
...
end
class Project < ActiveRecord::Base
has_many :permissions
has_many :users, :through=>:permissions
end

Can "belongs_to" be defined more than once in Active_Record?

I am trying to write a database for a company in town. I am using Devise for authentication, and Forem for the forums of the site. I decided to just have one class, "Account" for the Devise authentication, which will have many different access types to the site.
The bulk of the users will be just customers, which are segregated by routes (not Rails routes, street routes). So I decided to have them have their own profile model.
I want to do this - Profile is linked to account, and to route. (Routes are named gmr_routes)
Is this code the proper way to do it? Documentation I've found hasn't told me I can't, but I just want to be sure....
class Profile < ActiveRecord::Base
attr_accessible :first_name, :last_name, :phone_number, :street_address
belongs_to :account
belongs_to :gmr_route
end
Account has a has_one relationship with Profile, and gmr_route has a has_many.
Is this right?
Bryan
Yes, that's perfectly acceptable. You need to remember to include a foreign key id on any model with a belongs_to.
So in the case you describe, you would have account_id:integer and gmr_route_id:integer in your migration, and include those in the attr_accessible call in your model

Should I use separate models for Users and Profiles in Ruby on Rails 3 for the associations?

At first I had in mind just a User model for the authentication on my application but then i Decided to give this users a profile so I created the Profile model, now:
User
has_one :profile
and
Profile
belongs_to :user
Is this correct or should I just keep one model, let's say User, and keep there all the fields corresponding to profile and delete the Profile model?
Thanks.
== Update
This are the attributes of User and Profile Models.
User
username
password
Profile
name
last_name
email
website
picture
about
Having the user model with the authentication attribute and the profile with the specific and personal information is alright.
There is not "right" way.
I think this one is ok.

Best way to model users that have roles with differing attributes in Rails?

I'm a Rails noob and am hoping someone can help me wrap my head around this issue. I have an app that has a single User model using Authlogic for authentication and CanCan for authorization. There are three roles: Consumer, Business, and Admin. A user can have any number of these roles.
Businesses have additional attributes, however, and I need to model this such that I can add roles that each have potentially different attributes. Instinct tells me that I need to have a separate model to represent each role's attributes, i.e. BusinessRoleProfile, ConsumerRoleProfile, etc and then maybe mixin a module programmatically that adds the appropriate has_one reference(s) to the profile model(s).
Is this the best way to handle the separate attributes? If so, can someone guide me through how to dynamically include those mixins based on what role the user has?
EDIT:
Did some more research, this may help you. https://github.com/thefrontiergroup/scoped_attr_accessible
Looks like you can do things like:
class User < ActiveRecord::Base
# All attributes are accessible for the admin scope.
attr_accessible :all, :scope => :admin
# The default scope can only access a and b.
attr_accessible :a, :b
# Make both :c and :d accessible for owners and the default scope
attr_accessible :c, :d, :scope => [:owner, :default]
# Also, it works the same with attr_protected!
attr_protected :n, :scope => :default
end
OLD ANSWER
Looks like it may be featured in CanCan 2.0.
https://github.com/ryanb/cancan/issues/326