# ActiveResource, how to add a param to every request?

**URL:** <https://www.ruby-forum.com/t/activeresource-how-to-add-a-param-to-every-request/186742>\
**Category:** Rails\
**Created:** [February 25, 2008, 6:03pm UTC](https://www.ruby-forum.com/t/activeresource-how-to-add-a-param-to-every-request/186742 "2008-02-25T18:03:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ss\_1982](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@ss\_1982](https://www.ruby-forum.com/u/ss_1982)\
**Post date:** [February 25, 2008, 6:03pm UTC](https://www.ruby-forum.com/t/activeresource-how-to-add-a-param-to-every-request/186742/1 "2008-02-25T18:03:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ss\_1982](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@ss\_1982](https://www.ruby-forum.com/u/ss_1982)\
**Post date:** [February 25, 2008, 9:37pm UTC](https://www.ruby-forum.com/t/activeresource-how-to-add-a-param-to-every-request/186742/2 "2008-02-25T21:37:55Z")

</div>

On 2/25/08, Scott A S [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![ss\_1982](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@ss\_1982](https://www.ruby-forum.com/u/ss_1982)\
**Post date:** [February 25, 2008, 10:34pm UTC](https://www.ruby-forum.com/t/activeresource-how-to-add-a-param-to-every-request/186742/3 "2008-02-25T22:34:55Z")

</div>

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/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![ss\_1982](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@ss\_1982](https://www.ruby-forum.com/u/ss_1982)\
**Post date:** [February 26, 2008, 10:48am UTC](https://www.ruby-forum.com/t/activeresource-how-to-add-a-param-to-every-request/186742/4 "2008-02-26T10:48:29Z")

</div>

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](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
