How do I create a migration file which will create a user with the role of "admin"? I've written the following:
rails g migration User
How can I give a role of "admin" to this user?
Migrations may serve two functions:
The manipulation of structure in your database
The manipulation of data in your database
Generally speaking it is best practice to use them for the first, and to try to avoid the second (which may cause issues to an upgrade path down the line).
Your question is unclear regarding whether you're asking to create a User table with a column representing administrative privileges, or if you're trying to add an administrative user to an existing Users table.
If you are trying to create a User table, you will want to do the following:
rails g model users is_administrator:boolean other_column:type
Something along these lines will generate a migration (and matching model) allowing Users to be created, with a column containing what could be treated as administrative privileges.
On the other hand, if you already have a Users table, and are trying to add the ability to distinguish administrators from non-administrators, you would be better suited by something like this:
rails g migration add_is_administrator_to_users is_administrator:boolean
Finally, if you are asking how to add a User to the table Users which already exists and contains the column "is_administrator", I would encourage you to add the following line to db/seeds.rb:
User.create( is_administrator: true, other_column: 'other value' )
And then run the following line in the console
rake db:seed
If this doesn't make sense, I would encourage you to read up in Active Record Migrations, or (in the case that you choose to leverage the third method) Active Record Seeds.
You can have users and admin in same table and add one more column called "role" which will be boolean, which will decide if the current user is admin or not
rails g migration user role:boolean name
if role is false than it is not an admin.
Related
I'm developing and application with ruby on rails . i have the following models : user, role, option, permission. Depending on the Role of an User i want the menu of the application to display certain options. So an USER has one ROLE, one ROLE has one or many OPTIONS (availables in the menu),one OPTION can be assigned to many Roles . that's why i created a join table called PERMISSION which has rol_id , option_id and status.
SO, in the app, i want to be able to create a new role and check from a list the options this Role can have. But i don't know how to do a form that let me handle all this information and assign the role_id and the option_id the join table needs.
"Best" Solution:
Code it yourself, you should definitely become more familiar with ActiveRecord and be comfortable utilizing relationship tables and the roles one may have, along with writing helper functions like "is_an_admin?" or "is_a_moderator?"
You should also be comfortable with the routes and controllers for adding new users and checking permissions for can POST / UPDATE / PATCH / DELETE entries on your roles database.
Some db like "roles" where you store a user_id and role_level (1 is super_admin, 2 admin, or 3 moderator etc etc) and a user "has_one :role" association?
So my honest recommendation would be to learn it properly, here's some resources:
CULTTT Implementing Roles and Permissions
Association Basics
Easiest Solution
Use a gem, some options:
rolify (https://github.com/RolifyCommunity/rolify)
CanCanCan (https://github.com/CanCanCommunity/cancancan)
Pundit (https://github.com/elabs/pundit)
This is just a question out of my own curiosity, I would assume that it stores them in the user's table, but maybe it doesn't perhaps this isn't secure? if it doesn't store it in the users table where does it go, and if you know...why?
Thanks :)
Devise stores passwords and other data inside the table associated with the model you specify during setup. During setup you pass it a model name:
# MODEL is a placeholder for the model you want to use with Devise, usually 'User'
rails generate devise MODEL
The generator then creates a migration that alters the table associated with that model. So if you passed it User, the users table will be altered.
Have a look at the migration files inside Devise.
I'm creating a simple webapp that tracks calories as a way to learn Ruby on Rails. I'm using the gem devise for users to sign in. My next step is to generate a model. I was going to use:
rails generate model Tracker calories:integer consumed_on:datetime
The problem though is that I don't know how to relate this data to the signed in user. What am I missing from this generate model command?
rails generate model Tracker user_id:integer calories:integer consumed_on:datetime
This will generate your model with a foreign key in the database referencing the user table.
Of course this is not all you have to do, you have to put other code in your model and controller to combine those two columns, create the views, update the config/routes.rb file ..
Also:
Don't forget to run the rake db:migrate to make the relational table in the database.
You can see how its done here (your Tracker model/table is theirs Micropost):
http://ruby.railstutorial.org/chapters/a-demo-app#sec-microposts_resource
I have installed Devise and created Users and also Admins using Option 1 from this tutorial https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-role
Now I need a little help. When adding the Admins it creates a different table, and the potential arises that the admins and regular users could have the same ID's.
So how exactly do I got about grabbing information from users and admins? Say for example I want to display all users? Do I have to traverse the users table and then the admins table?
Or if I am displaying a post. How will I know which table to look for to get the user or admin info?
I know I can just add a role column to the users table but I wanted to avoid this.
I hope this makes sense haha
I highly recommend using the Single Table Inheritance (STI). You could do the STI by adding a column named type with a string datatype in the users table and create an admin model as well as a normal_user model both models will inherit from the user model of the devise gem.
class NormalUser < User
end
class Admin < User
end
The type column is a reserved word which will hold a value either NormalUser or Admin according to the user type you created.
To create an admin use Admin.create(...) and to create a normal user use NormalUser.create(...) where the dots are the attributes of the Admin and the NormalUser
Say I have two models of an existing Rails 3 application that I want to create an association for:
User
AccountType
I want to create an association such that I can do:
User.account_type
AccountType.users
So a user has a single account_type, and a account type has 0 or many users.
At the db level, the user will have a account_type_id and the AccountType table will not have any association related columns for User.
So my first step would be to write a test right? So in both of my user_spec.rb and account_type_spec.rb I should create a simple test to see if it has the proxy class .account_type an account_type.users exist right? anything else?
Modify the User.rb model and add belongs_to :AccountType, and in AccountType.rb add a has_many right?
Create a migration script, do I just add account_type_id or do I use a special way to reference the AccountType?
Regarding testing: i'd agree with Dave Newton's comment that you should test whatever behaviour you intend to create with this association (so write tests for the ability to create an association, to look up a user or account by this association, etc) but that testing for the existence of the methods as you describe is maybe going farther than necessary.
Your steps 2 and 3 look right to me.
rails g migration add_account_type_id_to_users account_type_id:integer
should generate the migration you need.