What is the best way to check if the “Admin” Group is in the user groups (using has_and_belong_to_many relationships) in Rails 3?
-
18-05-2021 - |
Question
I just want to check if the "Admin" Group is in the user groups. User and Group model has a has_and_belong_to_many relationship.
I had the system working fine with rails in developer mode, but when I tried to pass to production, this line in my "game" controller did not work:
if current_user.groups.where(:name => "Admin") != []
ActiveRecord::StatementInvalid (Mysql2::Error: Table 'db_production.groups' doesn't exist: SHOW FIELDS FROM `groups`)
Also, the same query works fine in the rails console:
irb(main):001:0> User.find(1).groups.where(:name =>
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`i
Group Load (0.4ms) SELECT `groups`.* FROM `groups` INNER JOIN `groups_users` ON `groups`.`id` = `groups_users`.`group_id` WHERE `groups_users`.`user_id` = 1 AND `groups`.`name` = 'Admin'
=> false
The server has Rails 3.2.0 and uses Phusion Passenger version 3.0.11 with Apache. I use device gem for the user management.
Solution
Clear from the error message that groups
table does not exist in db_production
database.
Mysql2::Error: Table 'db_production.groups doesn't exist
Probably your migration might have failed in production server.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow