simple_enum: Upgrading from 1.x to 2.x, missing accessors
Iβm currently in the process of upgrading my Rails application from 3.2
to 4.0
(and upwards to 5.1 but thatβs another story). As part of that upgrade, I changed simple_enum
version from 1.6.9
to 2.3.1
.
I find that newer accessors are not working, especially scopes.
Here is my model
class Season < ActiveRecord::Base
as_enum :name, low: 0, medium: 1, high: 2
end
When I try finding the low
season (or seasons), it will fail
Season.low #fail
Season.lows #fail
# error
NoMethodError: undefined method `lows' for #<Class:0x007f8415a11f08>
I believe it is because the accessor method where not generated at the time of the migration. Is there a way I can make it work?
I managed to get it working using the column_name but I believe it limits the use of simple_enum
π
Season.where(name_cd: 0)
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 21 (4 by maintainers)
Commits related to this issue
- fix: define `pluralize_scopes` in a non controversial way - so it doesn't get seen as an option - @link https://github.com/lwe/simple_enum/issues/139#issuecomment-378235776 — committed to nicolasrouanne/simple_enum-singular-scopes by nicolasrouanne 6 years ago
- fix(simple_enum): use newly discoverd syntax to specify custom column names - @link https://github.com/lwe/simple_enum/issues/139#issuecomment-378519052 — committed to nicolasrouanne/simple_enum-singular-scopes by nicolasrouanne 6 years ago
Great news, @drobny this fixes it π Thanks a lot.
I updated my sample repository to reflect the different possibilities.
Here is an extract:
EDIT: Fixed! β
The issue was solved by @drobny; it was caused by
pluralize_scopes
getting confused as an option.The solution is either to add options as an array, this maps to default arrays indices starting at 0
Or to add options as a hash specifying the value to map to the enum
Actually @nicolasrouanne you could do something like
with the current implementation which should allow you to do what you are describing π