# AR Associations- Am I missing something?

**URL:** <https://www.ruby-forum.com/t/ar-associations-am-i-missing-something/76507>\
**Category:** Rails\
**Created:** [November 19, 2006, 9:35pm UTC](https://www.ruby-forum.com/t/ar-associations-am-i-missing-something/76507 "2006-11-19T21:35:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cwk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@cwk](https://www.ruby-forum.com/u/cwk)\
**Post date:** [November 19, 2006, 9:35pm UTC](https://www.ruby-forum.com/t/ar-associations-am-i-missing-something/76507/1 "2006-11-19T21:35:24Z")

</div>

I am doing a pilot eval of Rails for my company. We’re using it to build  
a reporting module on top of a mysql database which is currently  
populated by a [ASP.NET](http://ASP.NET) application.

One thing I am wondering about is the utility of ActiveRecord  
associations, and whether I am missing some simple trick in their use.

Assume I have a User and a Post. Users can have many posts, and Posts  
have characteristics like a datestamp, rating, word count, deleted flag,  
etc.

While it is beautifully easy to do john.posts and get _all_ of John’s  
posts, what I almost always need to do is get john.posts(not\_deleted and  
post\_date=today) or something like that.

Granted, I’ve figurd out how it _can_ be done e.g. Posts.find(john,  
not\_deleted, date…) but by the time I’m done it seems like all I’m  
getting from ActiveRecord is a less top-heavy interface for SQL result  
sets, which is a nice thing for sure. But, I’m feeling a bit let down as  
the need to select associated objects with conditions beyond just the  
association is pervasive, which means I am back to writing lots of  
get\_foo methods and doing sql construction.

Any thoughts or suggestions?

---

<div class="post-metadata">

**Author:** ![cwk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@cwk](https://www.ruby-forum.com/u/cwk)\
**Post date:** [November 19, 2006, 11:29pm UTC](https://www.ruby-forum.com/t/ar-associations-am-i-missing-something/76507/2 "2006-11-19T23:29:30Z")

</div>

Cw K. wrote:

> While it is beautifully easy to do john.posts and get _all_ of John’s  
> posts, what I almost always need to do is get john.posts(not\_deleted and  
> post\_date=today) or something like that.

You can define a :conditions attribute when you find associated objects,  
or on the association call itself.

john.posts.find(:all, :conditions =\> [‘not\_deleted = ?’, true])

Or even better

class User \< ActiveRecord::Base  
has\_many :posts,  
:conditions =\> [‘not\_deleted = ?’, true]

```
has_many :all_posts,
         :class_name => 'Post',
         :foreign_key => 'user_id'

```

end

This way john.posts will always conform to the defined conditions. And  
if you want all posts, regardless of not\_deleted status, you can call  
john.all\_posts. Just note that you have to tell it the class\_name and  
foreign\_key of the association since it can’t be directly inferred form  
“all\_posts”.

---

<div class="post-metadata">

**Author:** ![cwk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@cwk](https://www.ruby-forum.com/u/cwk)\
**Post date:** [November 20, 2006, 4:14am UTC](https://www.ruby-forum.com/t/ar-associations-am-i-missing-something/76507/3 "2006-11-20T04:14:50Z")

</div>

Alex- thanks for that tip, that fills in some big gaps. However, I ran  
into one snafu:

Alex W. wrote:

> Cw K. wrote:
> 
> > While it is beautifully easy to do john.posts and get _all_ of John’s  
> > posts, what I almost always need to do is get john.posts(not\_deleted and  
> > post\_date=today) or something like that.
> 
> You can define a :conditions attribute when you find associated objects,  
> or on the association call itself.
> 
> john.posts.find(:all, :conditions =\> [‘not\_deleted = ?’, true])

When I try this I get an error: ArgumentError: wrong number of arguments  
(2 for 1)

The method below however works fine 🙂

> Or even better
> 
> class User \< ActiveRecord::Base  
> has\_many :posts,  
> :conditions =\> [‘not\_deleted = ?’, true]
> 
> ```
> has_many :all_posts,
> :class_name => 'Post',
> :foreign_key => 'user_id'
> 
> ```
> 
> end
> 
> This way john.posts will always conform to the defined conditions. And  
> if you want all posts, regardless of not\_deleted status, you can call  
> john.all\_posts. Just note that you have to tell it the class\_name and  
> foreign\_key of the association since it can’t be directly inferred form  
> “all\_posts”.

---

<div class="post-metadata">

**Author:** ![cwk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@cwk](https://www.ruby-forum.com/u/cwk)\
**Post date:** [November 20, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/ar-associations-am-i-missing-something/76507/4 "2006-11-20T14:45:41Z")

</div>

class User \< ActiveRecord::Base  
has\_many :not\_deleted\_posts, :class\_name =\> “Post”, :conditions =\>  
“not\_deleted is true”  
has\_many todays\_post, :class\_name =\> “Post”, :conditions =\> “date =  
today”  
end

Is this what you’re looking for ?

Thanks,  
Pratik

On 11/19/06, Cw K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> etc.  
> association is pervasive, which means I am back to writing lots of  
> get\_foo methods and doing sql construction.
> 
> Any thoughts or suggestions?
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
rm -rf / 2\>/dev/null - [http://null.in](http://null.in)

Dont judge those who try and fail, judge those who fail to try…

---

<div class="post-metadata">

**Author:** ![cwk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@cwk](https://www.ruby-forum.com/u/cwk)\
**Post date:** [November 20, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/ar-associations-am-i-missing-something/76507/5 "2006-11-20T14:45:56Z")

</div>

Hi –

On Sun, 19 Nov 2006, Alex W. wrote:

> Cw K. wrote:
> 
> > While it is beautifully easy to do john.posts and get _all_ of John’s  
> > posts, what I almost always need to do is get john.posts(not\_deleted and  
> > post\_date=today) or something like that.
> 
> You can define a :conditions attribute when you find associated objects,  
> or on the association call itself.
> 
> john.posts.find(:all, :conditions =\> [‘not\_deleted = ?’, true])

If you’ve defined not\_deleted as a boolean column you should also be  
able to do:

:conditions =\> “not\_deleted”

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![cwk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@cwk](https://www.ruby-forum.com/u/cwk)\
**Post date:** [November 20, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/ar-associations-am-i-missing-something/76507/6 "2006-11-20T14:45:54Z")

</div>

On 11/19/06, Alex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Just note that you have to tell it the class\_name and  
> foreign\_key of the association since it can’t be directly inferred form  
> “all\_posts”.

foreign\_key won’t be necessary if it’s user\_id.

-Pratik

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
rm -rf / 2\>/dev/null - [http://null.in](http://null.in)

Dont judge those who try and fail, judge those who fail to try…

---

<div class="post-metadata">

**Author:** ![cwk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@cwk](https://www.ruby-forum.com/u/cwk)\
**Post date:** [November 20, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/ar-associations-am-i-missing-something/76507/7 "2006-11-20T14:46:15Z")

</div>

Hi –

On Mon, 20 Nov 2006, Cw K. wrote:

> > You can define a :conditions attribute when you find associated objects,  
> > or on the association call itself.
> > 
> > john.posts.find(:all, :conditions =\> [‘not\_deleted = ?’, true])
> 
> When I try this I get an error: ArgumentError: wrong number of arguments  
> (2 for 1)

Whoops, I didn’t spot that problem when I advised about not bothering  
with the " = ?', true" part 🙂

> The method below however works fine 🙂
> 
> > Or even better
> > 
> > class User \< ActiveRecord::Base  
> > has\_many :posts,  
> > :conditions =\> [‘not\_deleted = ?’, true]

Now I’ll give my advice again, hopefully in a working context 🙂

has\_many :posts, :conditions =\> “not\_deleted”

No need to compare it to true.

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)
