sequenced icon indicating copy to clipboard operation
sequenced copied to clipboard

Building scopes doesn't work with ActiveRecord enums

Open alexdunae opened this issue 9 years ago • 7 comments

Thanks for the great gem. It's been working perfectly for me, up until when I tried to use enums as a sequence scope.

The issue is here: https://github.com/djreimer/sequenced/blob/master/lib/sequenced/generator.rb#L74

rel.where(c => record.send(c.to_sym))

Querying by enum value requires an integer, not a string value, and Rails' automatic typecasting is causing the query to return nothing.

Calling record.send(c.to_sym)) with an enum gives the string value of the enum, not the integer. Then the where(c => 'my_string_value') gets typecast to 0 before the query runs and nothing is returned (so all sequence_ids end up being 1).

alexdunae avatar May 04 '16 03:05 alexdunae

A quick and dirty fix seems to be

def build_scope(*columns)
  rel = yield
  columns.each { |c|
    val = record.send(c.to_sym)
    if record.respond_to?(:defined_enums) && record.defined_enums.key?(c.to_s)
      val = record.defined_enums[c.to_s][val]
    end
    rel = rel.where(c => val)
  }
  rel
end

but it's using a private defined_enums method from https://github.com/rails/rails/blob/master/activerecord/lib/active_record/enum.rb

Pretty gross.

alexdunae avatar May 04 '16 03:05 alexdunae

Seems like tests are running against a rails 3.1 app. Enum is 4.1 I believe, so it'd need to be updated or have a second test app generated. @djreimer thoughts?

mmartinson avatar May 24 '16 23:05 mmartinson

Yeah, we really should be testing against 4.x and higher. I'll try to get to this soon, but PRs welcome for that as well.

derrickreimer avatar May 25 '16 02:05 derrickreimer

Are you inclined to keep the tests for 3x as well or test solely against the latest stable? I might be able to get to this in a bit but would appreciate some input

On Tue, May 24, 2016, 7:34 PM Derrick Reimer [email protected] wrote:

Yeah, we really should be testing against 4.x and higher. I'll try to get to this soon, but PRs welcome for that as well.

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/djreimer/sequenced/issues/25#issuecomment-221458064

mmartinson avatar May 25 '16 02:05 mmartinson

I'm down to start a PR on this.

The first step seems to be to update the test suite for multiple Rails versions. The standard seems to be https://github.com/thoughtbot/appraisal -- do you have any preferred way of doing this?

alexdunae avatar Aug 03 '16 02:08 alexdunae

Friendly bump on this. I'm going to need to fork this for my own use and likely wouldn't add test coverage for older Rails if it's just for me.

If you've got a preferred method of multi-Rails-version testing that you'd like for a pull request let me know.

alexdunae avatar Aug 31 '16 03:08 alexdunae

Thanks for the bump @alexdunae. Totally cool with appraisal for multi-version testing. I'm fine with dropping Rails 3 support at this point.

derrickreimer avatar Aug 31 '16 13:08 derrickreimer

I've updated the test suite to run against Rails 5.2+ and Ruby 2.7+. I'm going to close this, but if anyone wants this picked up we can open a new issue, add a test case, and get it fixed permanently. 👍

excid3 avatar Aug 15 '22 19:08 excid3