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
I swapped out some enums I was using for some flags. Especially in tests where I had to set up specific models, I was changing code like this: model.update(enum_field: :enum_value) to model.update(flags: [:flag1, :flag2]) which causes a null value to get sent to the db.
Instead, I ended up doing something like model.update(flags: 3) with a magic number.
Yes, that would be beautiful! Please add some tests if you end up working on this. The master build on travis is now passing!
I see two options.
Option 1
Hash to indicate flags on or off setting:
model.update(flags: {flag1: true, flag2: false})
Option 2
Tokens to indicate the flag setting:
model.update(flags: [
:not_flag1,
:flag2
])
Right now I am leaning toward the Option 1 hash version because it approximates the way you would use a hash of boolean attributes more closely, and it seems more Railsy.
I swapped out some enums I was using for some flags. Especially in tests where I had to set up specific models, I was changing code like this:
model.update(enum_field: :enum_value)
tomodel.update(flags: [:flag1, :flag2])
which causes anull
value to get sent to the db.Instead, I ended up doing something like
model.update(flags: 3)
with a magic number.It looks like it might be possible to do something similar to enums (https://github.com/rails/rails/blob/a9dc45459abcd9437085f4dd0aa3c9d0e64e062f/activerecord/lib/active_record/enum.rb#L165). Is there any interest in that?
Or any other recommendations to avoid magic numbers when setting multiple flags at once (without having to have one line of code per flag)?
The text was updated successfully, but these errors were encountered: