# Parameterized ActiveRecord Associations: Any such thing?

**URL:** <https://www.ruby-forum.com/t/parameterized-activerecord-associations-any-such-thing/183276>\
**Category:** Rails\
**Created:** [February 11, 2010, 2:27am UTC](https://www.ruby-forum.com/t/parameterized-activerecord-associations-any-such-thing/183276 "2010-02-11T02:27:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zettabyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b19c9b/32.png) [@zettabyte](https://www.ruby-forum.com/u/zettabyte)\
**Post date:** [February 11, 2010, 2:27am UTC](https://www.ruby-forum.com/t/parameterized-activerecord-associations-any-such-thing/183276/1 "2010-02-11T02:27:01Z")

</div>

Hi list, how are ya?

So, my current project is just begging for the ability to have  
parameterized associations in my ActiveRecord classes. Basically I  
need something that is a cross between named scopes and standard  
associations (has\_many specifically).

I’m wondering if such a thing exists or, if not, if anyone else has an  
elegant, equivalent solution?

Example:

class Sprocket \< ActiveRecord::Base  
belongs\_to :widget  
belongs\_to :version  
end

class Widget \< ActiveRecord::Base  
has\_many :sprockets  
has\_many :versioned\_sprockets, lambda do |v|  
:conditions =\> { :version =\> v }  
end  
end

The idea behind the example is so I can do stuff like:

def get\_just\_the\_sprockets\_i\_need(callers\_conditions)  
version = Version.find(magic\_method\_returning\_just\_the\_right\_id)  
widget = some\_other\_magic\_method\_returning\_a\_widget\_instance  
widget.versioned\_sprockets(version).all(:conditions =\>  
callers\_conditions)  
end

I first considered just putting a named scope directly in the Sprocket  
class, giving me parameterized scoping. However, I specifically want  
to access my sprockets through a Widget, getting the implicit  
widget\_id scoping that comes with the association. If worse comes to  
worse, I’ll have an uglier named scope taking two parameters (does  
this work) instead of one:

class Sprocket …  
named\_scope :versioned, lambda do |widget, version|  
:conditions =\> { :widget =\> widget, :version =\> version }  
end  
…  
end

Any ideas?

Thanks.

---

<div class="post-metadata">

**Author:** ![zettabyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b19c9b/32.png) [@zettabyte](https://www.ruby-forum.com/u/zettabyte)\
**Post date:** [February 11, 2010, 3:29am UTC](https://www.ruby-forum.com/t/parameterized-activerecord-associations-any-such-thing/183276/2 "2010-02-11T03:29:03Z")

</div>

On Feb 11, 9:25 am, Kendall G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Example:  
> end
> 
> end  
> …  
> end
> 
> Any ideas?
> 
> Thanks.

Named scopes are already sufficient for what you’re trying to achieve:

class Sprocket  
named\_scope :versioned, lambda { |version| {:conditions =\> {:version  
=\> version}} }  
end

widget.sprockets.version(version).all(:conditions =\>  
callers\_conditions)

---

<div class="post-metadata">

**Author:** ![zettabyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b19c9b/32.png) [@zettabyte](https://www.ruby-forum.com/u/zettabyte)\
**Post date:** [February 11, 2010, 3:47am UTC](https://www.ruby-forum.com/t/parameterized-activerecord-associations-any-such-thing/183276/3 "2010-02-11T03:47:47Z")

</div>

On Feb 10, 7:27 pm, Erol F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Named scopes are already sufficient for what you’re trying to achieve:
> 
> class Sprocket  
> named\_scope :versioned, lambda { |version| {:conditions =\> {:version =\> version}} }  
> end
> 
> widget.sprockets.version(version).all(:conditions =\> callers\_conditions)

Cool, I didn’t know that the evaluated result (Array-like-thingy) of  
an association could in turn be used to access named scopes w/in the  
associated class. Works great.

Thanks.

---

<div class="post-metadata">

**Author:** ![zettabyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b19c9b/32.png) [@zettabyte](https://www.ruby-forum.com/u/zettabyte)\
**Post date:** [February 11, 2010, 3:18pm UTC](https://www.ruby-forum.com/t/parameterized-activerecord-associations-any-such-thing/183276/4 "2010-02-11T15:18:31Z")

</div>

On Feb 11, 2:46 am, Kendall G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Feb 10, 7:27 pm, Erol F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> Cool, I didn’t know that the evaluated result (Array-like-thingy) of  
> an association could in turn be used to access named scopes w/in the  
> associated class. Works great.

foo.sprockets is a proxy object, which in some cases will just behave  
like a named\_scope. Proxy extensions have been around for longer and  
can also be useful too, but they’re not needed as often these days.  
Proxy extensions are things like

has\_many :sprockets do  
def version(version)  
find(:all, :conditions =\> {:version =\>version)  
end  
end

In this case you gain nothing over using the named scope (in fact the  
named scope is more useful because you don’t have to be going through  
the association), but proxy extensions have access to the owner object  
of the association (proxy\_owner), the reflection object for the  
association etc. which can be useful for some purposes

Fred  
Fred
