Should I create two authetication Models - ruby-on-rails

I am developing a rails application which has basically two users. One is a typical user that posts questions and other is an expert that answers these questions (Answers can only be given by expert). An expert can only be created by approval of the developer/admin while anyone can sign_up to be a typical user.
I found various ways to approach this problem in Rails. What is the best approach to such a problem?
Should I create two separate authentication(devise) models?
Should I give any of the two separate roles(cancan) to a single user model.

Related

How to handle multiple Devise user models with 1 post model

I need some suggestions on building a website with Rails as a newbie to programming
I am learning Rails by myself for around 2 months and I now want to build a website to connect individual consultants with individual clients.
My first problem is I am going to create 3 user models by using Devise (one for clients and one for consultants and one for admin) with the following main condition:
Only users can post a question.
Only the posting user and consultants can have comments on the question.
Only user or admin can change the question's status (like
solved or unsolved)
This is because consultant model will require more information to be provided than the client (not only the information but I want the consultants to verify themselves by submitting certificates etc. before becoming a part of professionals on my platform). Any gem to handle this? Or this is better to be achieved by STI using only 1 user model?
Actually, what I actually want to do at the end would be more complicated (and I still have no idea how to create it at this time), what I want to know at this point is that how can I handle 3 different models with the only 1 posting model (especially foreign key) because I am used to creating 1 user model for 1 posting model (and add Boolean for further management).
In addition, if I want to implement the in-app chat function, can I use the action cable for the private chat between the consultant and the client or should I make a new model for the private conversation between them, or do you have any gem to recommend?
Appreciate any comments.
I am going to use STI to avoid any complication as provided in [https://stackoverflow.com/questions/9472852/devise-and-multiple-user-models?rq=1][1].

What is the name of this concept I am implementing, and how to in Rails?

I am trying to implement a feature to my project (kind of like a social media site) that could be either basic or complex and I am not sure if I am going to take forever reinventing the wheel or just go on a crazy tangent that won't work. I just need to "check in" so to say.
I am going to use Facebook terminology as an example to simplify the concept but implement similar features with different names. In Facebook you have Pages and Groups, which are similar yet have slight differences (from now, I will call the collection of these DataSets). Both of these can have multiple admins or followers, which are all User roles, and each User can have roles for multiple Groups and multiple Pages (one role per Group or Page). Then for example, you can click a drop down to change your account to post as a Page you are an admin for.
Essentially, the concept I am describing is where a single User can have a role for multiple different types of DataSets. For example, a single User could follow 30 different Pages and 10 different Groups, and be an admin for one Group and two Pages. Does the concept I am describing belong to a particular concept or software design pattern? I am finding it really hard to describe this feature without using Facebook examples.
I have a strategy to implement this type of functionality in Rails, but I feel like using this strategy would be making the problem harder than it is and there is a fancy rails way of doing it, or a Gem, but I just don't know how to research it due to lack of terminology to describe my problem.
Current strategy is:
I have a Users table from Devise. Pages and Groups are each individual models and have their own tables. I have matching database tables to make the many-to-many relationships between Pages and Users, along with Groups and Users (e.g. 3 column design, column for the user_id, column for the page_id and the type of relationship such as admin or follower). Let's call these Group_User and Page_User. I am being flexible at the moment as I may add more DataSets similar to Page and Group.
Then for the Devise User table, I have an extra two columns to track the DataSet that the User is an admin for and currently posting as. One column is for the DataSet type and the other for the id for this instance (e.g. [Group,1] is stored in these two columns to represent Group with group_id:1 and [Page,3] is used to represent Page with page_id:3). These two columns can be checked when displaying options relevant for admins in that Group/Page and a simple drop down at the top of the site changes the values in these columns to any of the Pages/Groups the logged in User is an admin for. This way, one User login can take on many admin roles and change between these easily as needed.
Is there a better way to do this in Rails, such as a gem or specific design pattern? Or am I on track to implement these features myself? I think I understand the problem but my solution just seems simple/raw and possibly might have unintended consequences later down the track (e.g. it seems database intensive).
One way I was thinking of doing this was making a concern that includes methods to build the relationships and pass in the name of the DataSet as an argument, just so I am not rewriting the same methods for Pages, then Groups, then whatever comes next.
I looked at other solutions such as polymorphic typing (which I think is good for if each user only had one role or only managed relationships for one group or one page) and Single Table Inheritance (but I think my Pages and Groups might be too different for this to work). I thought about using inheritance as well (e.g. a parent for both Group and Page) but I am not sure this helps much.
I am just a guy that studied too much computer science and not enough software engineering. Any tips on how to simplify this problem or just a simple "yeah that will work" would be really helpful!
I think you are going great in the database design. Once participated in a social media application like yours which had similar type of design. Your design seems much better than the one I worked with. In my opinion this type of applications are supposed to be database extensive.
There are several design patterns used in RoR. One I heavily use is Service Object Pattern to maintain thin controller and models. Also it helps me to write reusable class.
Another one I like is the Presenter Pattern to simplify views.
You can have a details look at this blog post for more design pattern ideas.

Role based authorizations or different tables in a ruby appointment booking app

I have seen regular debates about the way to manage the different class of users.
Usually, it seems that developers prefer a role based approach (e.g. user, admin,...) with gems like Cancancan
But I'm wondering if it's applicable for an appointment booking app (appointment for doctors, teachers,... or even bookings). Indeed in this case, the 2 types of users have access to totally different pages. In its documentation about associations, Ruby on Rails guide takes the example of a medical appointment booking app with 1 table for doctors and 1 table for patients.
For this kind of app, I'm a little bit lost regarding the most efficient solution!
Thanks!
You can use a tool like Cancanan to break out the different roles and abilities, then restrict access to certain parts of the system based on those rules.
Additionally you can display only the relevant navigation or pages when the user's accessing the system so they might not even be aware of what they're not seeing.

When is it okay to use multiple devise models?

When is it okay to use multiple devise models?
I have 3 types of users - users, vendors and admin. Vendors have a bunch more fields than users so I want to have separate tables for them. Users can sign up and sign in using facebook (vendors cannot). And users and vendors share the same sign-in page/form.
I started with multiple devise models, got confused how to handle a single sign-in page, and then read a lot here about using polymorphic associations and STI instead of discrete models. I'm still confused as to when each approach should be used and what would work better here... i know its a little vague, any advise would be great though, or any good links..
Users also fill out a bunch of extra fields when they sign up - even through facebook. How can I keep track of those fields for an omniauth login? (and should I use devise for this or something like omniauth identity..?)
Thanks for looking at this! I'm a rails newbie setting up authentication first time, really appreciate the help
Depending on the case it is recommended to separate or use a single model. The alternatives:
Use separate models, override the controllers, so firstly you check if the record match in User, and if it doesn't match try with Vendor
Use a single model for storing User and Vendor, use a boolean (a string if it is a polymorphic association) to check the kind of user, and add related models in order to store the additional fields
I think option #1 is easier but bigger, and option #2 is a little bit difficult but shorter.
Also, it would be a good idea to separate the models because User connects to FB and Vendor no, it represents a lot of differences.
In another way, the searches will be faster using option #2, because it will be only 1 query, and the table will be light because it will not contain the specific fields for users and vendors; you also have to consider this in order to make a decision.
For Admin you can follow a similar criteria.
Check this out: https://github.com/mkdynamic/omniauth-facebook

Creating groups and signing up users assigned to the group - Ruby on Rails

I'm building out a SaaS application that allows an individual to signup and create an exclusive Group. The admin will then be able to add people to the group by e-mailing them (this will be used primarily internally by companies).
Unfortunately, I'm quite new to Rails and thereby not too sure where to begin.
All groups should be mutually exclusive and users should only be able to belong to one group.
Any suggestions of where to begin in terms of the Group / user relationship?
Thanks a lot.
In terms of the relationship between Users and Groups, it will be a one (Group) to many (Users) relationship. Bringing rails into context, you can take a look at ActiveRecord associations to give yourself the right knowledge about the types of functionality that's exposed. Everyone user :belongs_to only one Group, and Groups :has_many Users. If you need a primer on database relationships, you can check out any primer like this or any link you can find with some diligent googling.
You can try out some things and come back once you have something going, then others will be able to help you out more. If you are REALLY new to Rails, you can check out and try the running example here. Good luck.

Resources