Reputation: 3998
I have an association something like below
User has_many roles
User has_one permission
Table names users, roles and permissions and they are under the same schema and database
I want to fetch the roles which are admin,super_admin,sub_admin. And want to update a column in the permission table
User.joins(:roles,:permission).where('roles.name in (?)', ['admin', 'sub_admin', 'super_admin']).select("permissions.*").update_all('permission.can_view_details', true)
Firing the above query is giving me error.
Caused by PG::UndefinedColumn: ERROR: column "can_view_details" of relation "users" does not exist LINE 1: UPDATE "schema"."users" SET "can_view_details" =...
Upvotes: 1
Views: 301
Reputation: 33460
Try getting the permissions right from the model, but using joins with User:
Permission.
joins(user: :roles).
where('users.roles IN (?)', %w[admin sub_admin super_admin]).
update_all(can_view_details: true)
Upvotes: 2