Rails 4: acts_as_list with complex scope

2.3k views Asked by At

My model has a string field, bar, that is either a, b, or c. It uses has_ancestry and acts_as_list. I want the list to be scoped on (1) the ancestry, and (2) bar, but I want a and b to be in the same list, but c in another list. That is, if bar is a or b then the Foo is active, and should in one list scope, and if bar is c, then it is inactive and should be listed separately.

Here is what I think the model might look like:

class Foo < ActiveRecord::Base
  scope :active, -> { where.not(bar: "c") }

  has_ancestry
  acts_as_list scope: [:ancestry, ???]
end

The question is, how do I get acts_as_list to scope two values of bar together?

(E.g. something like scope: 'ancestry = \'#{ancestry}\' AND bar = \'bar\'' would give me three different lists based on bar, not two.)

Let me know if I haven't explained this right. I looked pretty far into google and couldn't find the answer. Thanks!


Rails 4 and Ruby 2; latest versions of ancestry and acts_as_list, but that's flexible.

I should add that I have considered adding another database column that is equivalent to bar == "c" but I'm hoping for another solution.

1

There are 1 answers

0
Peter On BEST ANSWER

I ended up going with the additional database column with a before_save callback to set it based on the value in baz. I'm also considering switching to ranked-model (https://github.com/mixonic/ranked-model) instead of acts-as-list but that's another issue.