Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable logging in with STI subclass #26

Closed
wants to merge 1 commit into from

Conversation

iancanderson
Copy link

Say you have a User model using STI, with an Admin subclass.
Previously, if an Admin attempted to log in, their id would be saved in the admin_id key.
Then, when calling authenticate_by_cookie(User) (since we don't know the subclass of the user until the record is loaded), no user would be loaded, since it would look for the user_id key.

This changes the session keys to use the base_class, which is the root class of the STI hierarchy. That way, for the above scenario, the ID is both saved and loaded using the user_id key, so logging in works.

This shouldn't affect non-STI models, since base_class would just return the class itself

Say you have a `User` model using STI, with an `Admin` subclass.
Previously, if an `Admin` attempted to log in, their id would be saved in the `admin_id` key.
Then, when calling `authenticate_by_cookie(User)`, no user would be loaded, since it would look for the `user_id` key.

This changes the session keys to use the `base_class`, which is the root class of the STI hierarchy. That way, for the above scenario, the ID is both saved and loaded using the `user_id` key, so logging in works.

This shouldn't affect non-STI models, since `base_class` would just return the class itself
@mikker
Copy link
Owner

mikker commented Jun 20, 2018

This is great! Very nice description and solution 👍
Should we add a test case?

@mikker mikker mentioned this pull request Jun 22, 2018
@mikker mikker closed this in #27 Jun 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants