Search code examples
ruby-on-railsrubyruby-on-rails-4activeresource

ActiveResource toplevel constant issue; query path is different intermittently.


Currently I have the above mentioned issue.

From my understanding, this is an on going issue with rails autoloading and how there are standards in namespacing the various class.

Product which retrieve product/products without any scope.

# product.rb
class Product < ActiveResource::Base
  self.site = "#{end_point}/api/v2"
  ....
end

Market::Product which provide us an interface to seek product under the market scope, which is similar to a product.

# market/product.rb
class Market
  class Product < ::Product
    self.site = "#{end_point}/api/v2/markets/:market_name"
    ....
  end
end

Controller could call the market product object, but the object being return is just product

# market_product_controller.rb
class MarketProductController < ApplicationController
  def index
    @object = ::Market::Product.all
  end
  ....
end

On api, they are 2 different end-point, with 2 different result sets.

So far, when calling ::Market::Product, it seems like it is using ::Product url and :market_name as a params to that url.

Is there a good solution to this?

How did the rest of the community get around this issue?

Cheers for any help that is given.


Solution

  • Found the answer to my problem.

    http://blog.revathskumar.com/2013/12/activeresource-passing-prefix-options.html

    It would have seem that I have used activeresource incorrectly all these while.

    self.site = end_point
    self.prefix = '/api/v2/markets/:market_name/'
    

    This would be the right way to use it when it comes to nested resource.

    This solution would work nicely when it gets to ActiveResource::Base.rb:1029. It would be able to get the right prefix_parameters from the prefix_source and then create the right path to the remote end point.

    Hope this solution would help others who might encounter the same issue in the future.