Working with QueryObjects and modules in Rails

Viewed 31

Just starting working with Rails QueryObjects. I would like to group them in modules and submodules, but it doesn't work.

With a simple QueryObject test, it works:

class CountriesQuery
  def initialize()
  end

  def call()
    Country.where("id=1")
  end
end

In Controller:

@countries = CountriesQuery.new.call()

Now, I tried this:

module Shared
  module Countries
    class CountriesQuery
      def initialize()
      end

      def call()
        Country.where("id=1")
      end

    end
  end
end

My folder structure is queries > shared > countries > countries_query.rb

Controller:

@countries = Shared::Countries::CountriesQuery.new.call()

And the error is:

undefined method `where' for Shared::Country:Module

But Country is a model, nothing to do with a module...

So, how to use namespace/modules AND QueryObjects together ?

1 Answers

I suppose you have country.rb which implements the model of the same name.

If so, you want to reference the constant correctly:

::Country.where("id=1")
Related