# Can associations take arguments?

**URL:** <https://www.ruby-forum.com/t/can-associations-take-arguments/83515>\
**Category:** Rails\
**Created:** [January 26, 2007, 9:10pm UTC](https://www.ruby-forum.com/t/can-associations-take-arguments/83515 "2007-01-26T21:10:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [January 26, 2007, 9:10pm UTC](https://www.ruby-forum.com/t/can-associations-take-arguments/83515/1 "2007-01-26T21:10:55Z")

</div>

Let’s say an Author has many books. Can I say stuff like:  
author = Author.find :first  
author.books :order\_by =\> ‘publication\_date’  
or  
author.books :condition =\> [‘publication\_data \> ?’, somedate]

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [January 26, 2007, 9:42pm UTC](https://www.ruby-forum.com/t/can-associations-take-arguments/83515/2 "2007-01-26T21:42:02Z")

</div>

Like this:

author.books.find :all, :order =\> ‘publication\_date’  
author.books.find :all, :conditions =\> [ ‘publication\_date \> ?’,  
somedate]

Simon

---

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [January 26, 2007, 9:42pm UTC](https://www.ruby-forum.com/t/can-associations-take-arguments/83515/3 "2007-01-26T21:42:42Z")

</div>

likly. have you tried?

---

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [January 26, 2007, 10:33pm UTC](https://www.ruby-forum.com/t/can-associations-take-arguments/83515/4 "2007-01-26T22:33:14Z")

</div>

On Jan 26, 2:40 pm, Simon P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Like this:
> 
> author.books.find :all, :order =\> ‘publication\_date’  
> author.books.find :all, :conditions =\> [‘publication\_date \> ?’, somedate]

Awesome, that is what I was looking. I knew Rails could do it, I just  
couldn’t find the syntax. Thank you.

Next question… how smart/efficient is Rails about doing that query?  
Is the order and condition done in Ruby code or in SQL?

Thanks,  
– Christopher

---

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [January 26, 2007, 9:59pm UTC](https://www.ruby-forum.com/t/can-associations-take-arguments/83515/5 "2007-01-26T21:59:04Z")

</div>

You can also use association extensions

class Author \< AR::Base  
has\_many :books do  
def published\_on(d)  
find(:all, :conditions =\> [“published\_on = ?”, d])  
end

```
def published_before(d)
  find(:all, :conditions => ["published_on < ?", d])
end

def published_after(d)
  find(:all, :conditions => ["published_on > ?", d])
end

```

end  
end

author.books.published\_on(some\_date)  
etc…

keep in mind this only adds those methods to the association, not to  
the model itself.

Chris

---

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [January 27, 2007, 12:50am UTC](https://www.ruby-forum.com/t/can-associations-take-arguments/83515/6 "2007-01-27T00:50:30Z")

</div>

On 1/26/07, Christopher J. Bottaro [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Next question… how smart/efficient is Rails about doing that query?  
> Is the order and condition done in Ruby code or in SQL?
> 
> Thanks,  
> – Christopher
> 
> >

Christopher,

You can easily check the sql produced by running a tail -f against  
your development.log.

You also should read up on some of Jamis’ great recent posts:

[http://weblog.jamisbuck.org/2007/1/9/extending-activerecord-associations](http://weblog.jamisbuck.org/2007/1/9/extending-activerecord-associations)  
[http://weblog.jamisbuck.org/2007/1/18/activerecord-association-scoping-pitfalls](http://weblog.jamisbuck.org/2007/1/18/activerecord-association-scoping-pitfalls)

–  
Zack C.  
[http://depixelate.com](http://depixelate.com)

---

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [January 27, 2007, 11:05am UTC](https://www.ruby-forum.com/t/can-associations-take-arguments/83515/7 "2007-01-27T11:05:14Z")

</div>

On Jan 26, 2007, at 21:10 , Christopher J. Bottaro wrote:

> Let’s say an Author has many books. Can I say stuff like:  
> author = Author.find :first  
> author.books :order\_by =\> ‘publication\_date’  
> or  
> author.books :condition =\> [‘publication\_data \> ?’, somedate]

author.books.find(:all, :order =\> 'publication\_date)

and

author.books.find(:all, :conditions =\> [‘publication\_data \> ?’,  
somedate])

–  
Jakob S. - [http://mentalized.net](http://mentalized.net)
