My Profile model has a full_name attribute, but I want it to be set when the user signs up by the User create method. The profile and user are two separate models with two separate controllers. Can I do this? If so, how?
Wouldn't it make more sense to have full_name on User? Then each Profile will have a User and the Profile can just delegate the full_name accessor to its User; or the view could just use profile.user.full_name instead of profile.full_name.
Related
I want to split Devise user into different types? Like, for example: user go to the registration page and sign up, but based on whether they're a teacher or a student they will registered and login to see different navbar? How would I do that using a checkbox?
It is excellently explained in their Wiki.
In my opinion you should consider option number 1. and 3:
Separate model for student and teacher, if they have different attributes
One model for both with additional column role. It will be appropriate if models have the same attributes.
Then on your views just check what is the role / type of user and present proper content.
I read the two posts here and here, but still have trouble figuring out how everything is tied together. Basically I have 2 types of users, Trainers and Clients. They share some common attributes (email, phone, first name, last name, etc), but they will also have some custom attributes.
Assuming STI is the way to go, I would have 3 models:
User (devise)
Trainer (inherits from User)
Client (inherits from User)
When the user signs up, they should be able to use the same form and just select from a drop down if they're a trainer or client. Once the form is submitted how do I go about specifying the type of user that has just been created? Do I need logic in the controller to check the user type, and then run Trainer.create() or Client.create()?
How do I setup devise with more than one model?
I've already tried using rolify and cancan to setup separate roles in my database, but each role has a different way to authenticate themselves to login. For example, a student will have a student_number, and a lecturer will have a username but no student_number.
Also there is a bunch of other attributes a lecturer won't have that a student will and vice versa.
I'm new to rails 4.
It looks like classes and inheritance can come handy in this case.
What about defining a User mode and the let Student and Lecturer inherit from that class?
class Student < User
# student's peculiar attributes
end
class Lecturer < User
# lecturer's peculiar attributes
end
Then you can have two separate controllers and corresponding views. The the login page might have two links to the proper login pages.
I resolved this issue using a User model and a "has_one" Profile model attached to User, all users log in using the same table but devise load another data saved in Profile.
Other option is use just User model and left student_number empty when the user is not an student.
For example, I have a password generator in my user registration website. Where should I put the function of generating password?
Put together with UserController?
What is the correct way to put these functions?
I would recommend putting it into a class of its own. For the sake of SRP, your UserModel should do things with a User and only a User. Your UserModel class should not be responsible for generating passwords for new users. Separate it into its own class and call a method on that class during the creation of your new user in your UserModel.
I would put it in my User Model.
Or you could create a Utility class and put it in there.
Hi i have a table that is called contractors this is acting as the users model for logging into the system. I have a second table called employees. I have created the relationship between the two tables. contractors has_many employees and employees belong_to contractor.
The employee table has a field for contractor id as a foreign key.
When the contractor logs into how can i set the view to only show him the employees that belong to him
Thanks in advance!
I don't know how your authentication works, but sou should have something like current_user helper which retrieves the currently logged in user from session. Devise gem, for example, creates it automatically.
#employees = current_user.employees