You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It will store multi-enums data in a integer column, and if you don't want SimpleEnum::Multiple manage how you store your data, you can use multiple accessor:
This accessor will not handle how the data saved in the database, so you have to use something like serialize :favorite_cds, or implement your own Coder.
Working with multi-select enums
classUser < ActiveRecord::Baseas_enum:favorites,[:iphone,:ipad,:macbook],accessor: :join_tableendjane=User.newjane.favorites=[:iphone,:ipad]jane.iphone?# => truejane.ipad?# => truejane.macbook?# => falsejane.favorites# => [:iphone, ipad]jane.favorite_cds# => [0, 1]joe=User.newjoe.iphone!# => [:iphone]joe.favorites# => [:iphone]joe.favorite_cds# => [0]User.favorites# => #<SimpleEnum::Enum:0x0....>User.favorites[:iphone]# => [0]User.favorites.values_at(:iphone,:ipad)# => [0, 1]User.iphones# => #<ActiveRecord::Relation:0x0.... [jane, joe]># You can also do this since `favorites` returns a # #<SimpleEnum::Multiple::CollectionProxy> rather than a #<Array>:joe=User.newjoe.iphone!# => [:iphone]joe.favorites.push:ipadjoe.favorites# => [:iphone, ipad]joe.favorite_cds# => [0, 1]
License
The gem is available as open source under the terms of the MIT License.