ActiveResource, how to add a param to every request?

Hi,

I have just created a couple of ActiveResource classes that are being
used to communicate with a rest api.

In every request I am having to place an api key as an additional param
e.g.
@book = Book.find(params[:id], :params => {:key =>
‘7348d92f82836eb9d5f69c0’})

Is anyone aware of a dry’er solution that will allow me define the
additional param in the class below self.site or something similar?

Thanks,
Scott

On 2/25/08, Scott A S [email protected] wrote:

Is anyone aware of a dry’er solution that will allow me define the
additional param in the class below self.site or something similar?

Maybe something like this (untested):

class Book < ActiveRecord::Base

cattr_accessor :api_key

def find(*args, &blk)
with_options(:key => class.api_key) do
super
end
end

end

Then somewhere (in config?)

Book.api_key = ‘7348d92f82836eb9d5f69c0’


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Cool, Cheers Rick

class Book < ActiveRecord::Base

cattr_accessor :api_key

def find(*args, &blk)
with_options(:key => class.api_key) do
super
end
end


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

After looking into a way to allow an additional param to also be sent
for create and update requests, I have come across a patch that does
exactly this.

http://dev.rubyonrails.org/ticket/10088

Does anyone know when this is likely to make its way into edge?

Cheers,
Scott

Scott A S wrote:

Hi,

I have just created a couple of ActiveResource classes that are being
used to communicate with a rest api.

In every request I am having to place an api key as an additional param
e.g.
@book = Book.find(params[:id], :params => {:key =>
‘7348d92f82836eb9d5f69c0’})

Is anyone aware of a dry’er solution that will allow me define the
additional param in the class below self.site or something similar?

Thanks,
Scott