2

Using ActiveResource and when I have a nested resource, using the 'prefix' works great.

class Account < ActiveResource::Base
  self.prefix = "/users/:user_id/"
end

All is fine as long as :user_id has been defined, or else an error will be raised. But how to make the 'self.prefix' conditional, in cases where I don't want to access this resource as a nested resource, but rather as the resource itself? For example, I'd like to retrieve all accounts, not just the accounts scoped by a particular user?

Carson Cole
  • 4,183
  • 6
  • 25
  • 35

1 Answers1

2

You could set the prefix to be entirely dynamic:

class Account < ActiveResource::Base
    self.prefix = ":prefix_path"
end

Then set it at runtime:

Account.find(:all, :params => { :prefix_path => '/users/4' } )
coneybeare
  • 33,113
  • 21
  • 131
  • 183