I am using Test to test my application. I have some fixtures: users.yml, roles.yml, roles_users.yml. The users and roles are loaded but not the many to many table roles_users so the users are not related to any role. With Rails console in development everything is ok, in test any user is not connected to any role. I can see the users and roles in the test database but roles_users is empty
Do I have to specify somewhere how to load this fixture?
If you're using Rails >=2.2 and a standard HABTM association between users and roles, you shouldn't need the roles_users.yml file. Instead, add a roles line for each user in users.yml:
sally:
roles: admin, editor
...
fred:
roles: basic
...
The values are the names of your role fixtures. I'm not completely sure this will solve your problem, but it's cleaner at the very least.
Related
For the Rolify gem in Rails in order to add a role to a user you can do:
user = User.find(1)
user.add_role :admin
But I have a large database of already existing users with no roles. How do I add a role to multiple users with a single command?
I tried the snippet below but it errored:
users = User.where(email:['email1','email2'])
users.addrole :admin
Does anyone know how to do this? Or do I need to create a script that cycles through the users automatically and assigns a role one by one?
Rolify is mostly just a quick and dirty role system, and it currently doesn't provide this feature.
You'll have to write a bulk insert/upsert query of your own into the database tables that Rolify persists data, e.g. (roles and users_roles for a users table).
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)
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.
I have many models using a has_many_through relationship: users, roles, and security_items.
A user can be in multiple roles, a role can have many security items. So if a user is in multiple roles, how can I find out if a particular item is any of the roles? Like if its true in one but false in the other, the true should take precedence.
If a user is only in one role then the following works, but if a user is multiple roles then the following fails in rails console
role = RoleMembership.where("user_id = ?", user.id)
role.security_items.exists?(1)
Error if the user is in multiple roles:
NoMethodError: undefined method `security_items' for #<ActiveRecord::Relation::ActiveRecord_Relation_RoleMembership:0x00000102da5e28>`
How can I check each and every role to see if the item exists in the table?
Try role.map(&:security_items).flatten.uniq to get all the security items.
RoleMembership.where("user_id = ?", user.id).select { |role| role.security_items.present? }
Working upon the code that you put up, thats how I would do it.
EDIT:
If you get the NOMETHOD error, it means there isn't a method called security_items for the role. Is your relationship set up right in the models? You can try in the console
RoleMembership.find(1).security_items
If that errors out then you have identified your problem.
I'm creating a Rails app for students and high schools and I'm having some trouble with my User.rb.
I want to have a user model to be used for logging in, but having that user have many roles. The tricky part is that I want users that have a student role to have_one student page, and those that have a role of principal to have_one high_school page.
The students and also nested in the high_school so the entire thing becomes a big mess.
So my question(s): How do I limit a user to only creating one student / high school to represent them? Also how would I nest this student pages inside the highschool without screwing up the user system?
My environment: Rails3 and Ruby 1.9.2dev
Thank you!
Follow up: Would it be possible to put the name of the high_school in the subdomain? That would make the url look like
highschoolname.mysite.com/students/eric-koslow
I'd suggest polymorphic association to user_representations. It'd hold info about which high_school object or which student_page to associate the appropriate user to.
You can made a validation to avoid the multi-creation.