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

Support ActiveRecord 4.x enums #196

Closed

Conversation

markpmitchell
Copy link
Contributor

Fixes Issue #102

ActiveRecord queries for records with enums are by integer value, not by symbol. As CanCan uses the same query syntax, the way to specify a condition based on enum values is like so:

can :read, Shape, color: Shape.colors[:blue]

While CanCan already generated the correct SQL queries for accessible_by, checks against individual objects did not work because the value of an enum attribute is a String. Therefore, the ActiveRecord 4 adapter must special case matches_condition? to convert the String to the appropriate integer before comparing against the value provided in the condition.

@confiks
Copy link

confiks commented Apr 2, 2015

I've been bumping into this issue as well. Thanks for the fix!

@kimrgrey
Copy link

Hey, guys! What about this PR? I think it's pretty important patch because it's not possible to use cancancan with enums right now.

@nelsonwittwer
Copy link

Would love to see more support for enums in CanCanCan. In the meantime, this is what I have been doing:

can :read, ::Media, :published? => true

@kimrgrey
Copy link

@nelsonwittwer, suppose that your approach will not work with accessible_by because cancan will try to make sql query with column :published? which not exists.

@jwg2s
Copy link

jwg2s commented Sep 29, 2015

👍 would love to see this get merged

@Senjai Senjai self-assigned this Sep 29, 2015
@Senjai Senjai modified the milestones: 3.x, 3.0, 2.0 Oct 8, 2015
@Senjai Senjai closed this in 5b361a4 Oct 8, 2015
@Senjai
Copy link
Member

Senjai commented Oct 8, 2015

@jwg2s @markpmitchell I rebased your changes and pushed them to master. Thank you for your contribution! Sorry it took a while to get around to.

@coorasse
Copy link
Member

coorasse commented Oct 8, 2015

👍

@kimrgrey
Copy link

kimrgrey commented Oct 8, 2015

Hey-ho! Great news! ❤️

@markpmitchell
Copy link
Contributor Author

Thanks!

Mark Mitchell

On Oct 7, 2015, at 8:19 PM, Richard Wilson notifications@github.com wrote:

@jwg2s https://github.com/jwg2s @markpmitchell
https://github.com/markpmitchell I rebased your changes and pushed them
to master. Thank you for your contribution! Sorry it took a while to get
around to.


Reply to this email directly or view it on GitHub
#196 (comment)
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants