ActiveResource with conditional prefix (self.prefix)?-Collection of common programming errors

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?

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