如何使用Squeel做嵌套查询?

How to use Squeel to make innested query?

我想使用 squeel 构建查询生成器,我是这样做的:

Parent class:

module ModelFilters
  class Base
    def initialize(user)
      @user = user
    end

    def build(method_name)
      head.where { my { send(method_name) } }
    end

    protected

    def head
    end
  end
end

Child class:

module ModelFilters
  class Collection < Base
    def __showable__
      not_private | author
    end

    protected

    def head
      ::Collection.joins(:collaborators)
    end

    private

    def not_private
      is_private == false
    end

    def author
      user_id == @user.id
    end
  end
end

最后是我的电话:

a = ModelFilters::Collection.new(user)
a.build(:__showable__)

实际上我的问题是我不知道 Squeel 如何用于嵌套查询,我的实际错误是 undefined local variable or method 'is_private'(很明显)。

有没有办法使用 Squeel(或其他 ActiveRecord 查询生成器)来构建类似的东西?

谢谢大家!

如果你这样做会怎样:

module ModelFilters
  class Base
    def initialize(user)
      @user = user
    end

    def build(method_name)
      head.where { |q| send(method_name, q) }
    end

    protected

    def head
    end
  end
end

module ModelFilters
  class Collection < Base
    def __showable__(q)
      not_private(q) | author(q)
    end

    protected

    def head
      ::Collection.joins(:collaborators)
    end

    private

    def not_private(q)
      q.is_private == false
    end

    def author(q)
      q.user_id == @user.id
    end
  end
end