There is a difference between how classes are loaded on production and dev and I kind figure our where does it come from.
I am using cancan. For an ability I have
module Abilities
class AccountsAbility
include CanCan::Ability
def initialize user
can :manage, User, id: user.id
end
end
end
In development this works
2.6.5 :001 > User.accessible_by(Abilities::AccountsAbility.new(User.first))
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL ORDER BY "users"."id" ASC LIMIT $1 [["LIMIT", 1]]
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL AND "users"."id" = $1 LIMIT $2 [["id", 2021270], ["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<User id: 2021270, created_at: "2020-03-19 23:56:24", updated_at: "2020-06-14 12:33:50", username: "0762003">]>
In Production the same code is
2.6.5 :022 > User.accessible_by(Abilities::AccountsAbility.new(User.first))
=> #<ActiveRecord::Relation []>
Does anyone have an idea what might be cause this? Rails 6.0.3.1, ruby 2.6.5
Update 1.
I checked that it works correctly on a brand new project, so it is probably something as a leftover from 5.2 to 6.0 migration. A configuration somewhere.
Update 2
A different class is loaded for the two envs. Yet there is only one class in the app
Production
2.6.5 :014 > Abilities::AccountsAbility.new(User.first)
=> #<Abilities::AccountsAbility:0x000055a09f497718 @rules=[#<CanCan::Rulecan [:manage], [Abilities::User], [], {:id=>1}>], @rules_index={Abilities::User=>[0]}>
Dev
User Load (1.2ms) SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL ORDER BY "users"."id" ASC LIMIT $1 [["LIMIT", 1]]
=> #<Abilities::AccountsAbility:0x000055e775295830 @rules=[#<CanCan::Rulecan [:manage], [User(id: integer, created_at: datetime, updated_at: datetime, username: string)=>[0]}>
Dev is loading User while production is loading Abilities::User, yet there is no such class.
Update 3
Turns out there is a Abilities::User module defined in the abilities that has and in a production env the Abilities::User is loaded when we call User.
module Abilities
module User
class SubscriptionsAbility
include CanCan::Ability
def initialize user
....
end
end
end
end
Not sure if this is the expected behavior though. We are calling for User in the Abilities module like
module Abilities
class AccountsAbility
include CanCan::Ability
def initialize user
can :manage, User, id: user.id
end
end
end
and when it sees User it loads Abilities::User instead of User.