Carson Cole
Carson Cole

Reputation: 4461

ActiveResource with conditional prefix (self.prefix)?

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?

Upvotes: 2

Views: 993

Answers (1)

coneybeare
coneybeare

Reputation: 33101

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' } )

Upvotes: 2

Related Questions