obfuscate_id
obfuscate_id copied to clipboard
Unable to find model with obfuscated ID via a has_many / through association
Somewhat related to #34.
Account model has obfuscated ID.
User.rb
has_many :accounts, through: :memberships
AccountsController#show
@account = current_user.accounts.find(params[:id])
Error
Couldn't find Account with 'id'=0364209871 [WHERE "memberships"."user_id" = ?]
Workaround suggested in #34 works fine with direct find i.e. Account.find_by_id(params[:id])
but fails to find the account via association.
+1
+2
I opened a Rails issue for that and asked if that is an error in ActiveRecord and if there is a better approach than overriding find: https://github.com/rails/rails/issues/21731
+3
I believe this implementation from friendly_id
can be used as a basis https://github.com/norman/friendly_id/blob/master/lib/friendly_id/finders.rb
https://github.com/norman/friendly_id/blob/master/lib/friendly_id/finder_methods.rb
+4
+5