# Association Hash Conditions

**URL:** <https://discuss.rubyonrails.org/t/association-hash-conditions/31869>\
**Category:** rubyonrails-core\
**Created:** [July 15, 2008, 6:24am UTC](https://discuss.rubyonrails.org/t/association-hash-conditions/31869 "2008-07-15T06:24:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nik\_Wakelin](https://avatars.discourse-cdn.com/v4/letter/n/bcef8e/32.png) [@Nik\_Wakelin](https://discuss.rubyonrails.org/u/Nik_Wakelin)\
**Post date:** [July 15, 2008, 6:24am UTC](https://discuss.rubyonrails.org/t/association-hash-conditions/31869/1 "2008-07-15T06:24:38Z")

</div>

Hi All,

I've got a patch ([http://rails.lighthouseapp.com/projects/8994/tickets/467-hash-conditions-belongs\_to-sugar](http://rails.lighthouseapp.com/projects/8994/tickets/467-hash-conditions-belongs_to-sugar)) that allows you to do:

Person.find(:all, :conditions =\> { :university =\> victoria\_wellington })

I'm planning on making it work for belongs\_to and has\_one (including polymorphic association).

The discussion was basically around whether or not we should tie hash conditions to the database field names.

There was also some discussion about find\_by\_association, i.e

&nbsp;&nbsp;Person.find\_by\_university(victoria\_wellington)

Josh has a patch that I can merge into all this stuff pretty easily.

Pratik asked me to restart the discussion here, so I'd be interested to hear what everybody thinks.

Thanks!

Nik

---

<div class="post-metadata">

**Author:** ![Michael\_Koziarski1](https://avatars.discourse-cdn.com/v4/letter/m/aca169/32.png) [@Michael\_Koziarski1](https://discuss.rubyonrails.org/u/Michael_Koziarski1)\
**Post date:** [July 15, 2008, 8:42am UTC](https://discuss.rubyonrails.org/t/association-hash-conditions/31869/2 "2008-07-15T08:42:13Z")

</div>

> Person.find(:all, :conditions =\> { :university =\> victoria\_wellington })

I'm quite suspicious where you're using find\_by\_some\_association, couldn't you be walking the associations back in the other direction.

victoria\_wellington.people

> I'm planning on making it work for belongs\_to and has\_one (including polymorphic association).

How are you planning on making it work for has\_one, what would the query look like?

> The discussion was basically around whether or not we should tie hash conditions to the database field names.
> 
> There was also some discussion about find\_by\_association, i.e
> 
> Person.find\_by\_university(victoria\_wellington)
> 
> Josh has a patch that I can merge into all this stuff pretty easily.
> 
> Pratik asked me to restart the discussion here, so I'd be interested to hear what everybody thinks.

I'd be a little concerned about adding some special case code to the sanitize\_sql logic, but if some work was undertaken to rationalise all that stuff at the same time then this kind of thing could be a nice addition.

Good stuff

---

<div class="post-metadata">

**Author:** ![Lawrence\_Pit](https://avatars.discourse-cdn.com/v4/letter/l/b9bd4f/32.png) [@Lawrence\_Pit](https://discuss.rubyonrails.org/u/Lawrence_Pit)\
**Post date:** [July 15, 2008, 11:19am UTC](https://discuss.rubyonrails.org/t/association-hash-conditions/31869/3 "2008-07-15T11:19:35Z")

</div>

> > ```ruby
> > Person.find(:all, :conditions => { :university => victoria_wellington })
> > 
> > ```
> 
> ```ruby
> 
> I'm quite suspicious where you're using find_by_some_association,
> couldn't you be walking the associations back in the other direction.
> victoria_wellington.people
> 
> ```

Can you explain this some more?

Why does has\_many acts as a named\_scope and belongs\_to not?

I think it would be nice to be able to do:

Comment.university(boston).creator(current\_user).commentable(post).all

Instead of:

Comment.find\_all\_by\_university\_id\_and\_creator\_id\_and\_commentable\_type\_and\_commentable\_id(boston, current\_user, post.class.base\_class.name.to\_s, post.id)

It’s main advantage would be that it allows easy formation of dynamic queries based on input:

```
scope = Comment.scoped({:include => [:creator, :commentable]})

scope = scope.commentable(post) if post

scope = scope.university(params[:uni]) if params[:uni]

scope = scope.creator(current_user) if params[:created_by_me] == "1"

scope = scope.country(params[:country]) if params[:country]

@comments = scope.all

```

Lawrence

---

<div class="post-metadata">

**Author:** ![Michael\_Koziarski1](https://avatars.discourse-cdn.com/v4/letter/m/aca169/32.png) [@Michael\_Koziarski1](https://discuss.rubyonrails.org/u/Michael_Koziarski1)\
**Post date:** [July 15, 2008, 11:28am UTC](https://discuss.rubyonrails.org/t/association-hash-conditions/31869/4 "2008-07-15T11:28:46Z")

</div>

> I think it would be nice to be able to do:
> 
> &nbsp;&nbsp;Comment.university(boston).creator(current\_user).commentable(post).all

Indeed, that does look pretty nice, and very useful in some circumstances. But it could be implemented without touching the query generation code at all. You could automatically add proc based named scopes to the class when you define the associations. and allow those procs to take either ids or instances.

I'd definitely prefer that approach to supporting hash keys referring to associations inside the query generation code.

> Comment.find\_all\_by\_university\_id\_and\_creator\_id\_and\_commentable\_type\_and\_commentable\_id(boston, current\_user, post.class.base\_class.name.to\_s, post.id)

I'd never advocate anything quite as ugly as that. but something like

post.comments\_by\_user\_about(current\_user, boston)

would work fine too.

> It's main advantage would be that it allows easy formation of dynamic queries based on input:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;scope = Comment.scoped({:include =\> [:creator, :commentable]}) &nbsp;&nbsp;&nbsp;&nbsp;scope = scope.commentable(post) if post &nbsp;&nbsp;&nbsp;&nbsp;scope = scope.university(params[:uni]) if params[:uni] &nbsp;&nbsp;&nbsp;&nbsp;scope = scope.creator(current\_user) if params[:created\_by\_me] == "1" &nbsp;&nbsp;&nbsp;&nbsp;scope = scope.country(params[:country]) if params[:country] &nbsp;&nbsp;&nbsp;&nbsp;@comments = scope.all

This is a nice use case, care to work with nik on implementing it?

---

<div class="post-metadata">

**Author:** ![Nik\_Wakelin](https://avatars.discourse-cdn.com/v4/letter/n/bcef8e/32.png) [@Nik\_Wakelin](https://discuss.rubyonrails.org/u/Nik_Wakelin)\
**Post date:** [July 16, 2008, 12:04am UTC](https://discuss.rubyonrails.org/t/association-hash-conditions/31869/5 "2008-07-16T00:04:09Z")

</div>

> > Person.find(:all, :conditions =\> { :university =\> victoria\_wellington })
> 
> I'm quite suspicious where you're using find\_by\_some\_association, couldn't you be walking the associations back in the other direction.
> 
> victoria\_wellington.people

You could, but that's less easy when you've got other conditions. It's just meant to replace:

Person.find(:all, :conditions =\> { :university\_id =\> victoria\_wellington })

Rails will automatically use the id for "victoria\_wellington", but it seems weird to say "\_id" in the hash when you \_actually\_ mean the association. And it gets even weirder when your foreign keys aren't named the same as your association.

> > I'm planning on making it work for belongs\_to and has\_one (including polymorphic association).
> 
> How are you planning on making it work for has\_one, what would the query look like?

At first it seemed hard as the has\_one has to query against a seperate table, but Pratik introduced a patch that allows you to specify multiple tables in the conditions hash. I was planning on using that, but I'll freely admit I haven't looked into it properly yet 😉

> > The discussion was basically around whether or not we should tie hash conditions to the database field names.
> > 
> > There was also some discussion about find\_by\_association, i.e
> > 
> > Person.find\_by\_university(victoria\_wellington)
> > 
> > Josh has a patch that I can merge into all this stuff pretty easily.
> > 
> > Pratik asked me to restart the discussion here, so I'd be interested to hear what everybody thinks.
> 
> I'd be a little concerned about adding some special case code to the sanitize\_sql logic, but if some work was undertaken to rationalise all that stuff at the same time then this kind of thing could be a nice addition.

What kind of rationalisation needs to happen? I'm happy to take some of that on.

---

<div class="post-metadata">

**Author:** ![Nik\_Wakelin](https://avatars.discourse-cdn.com/v4/letter/n/bcef8e/32.png) [@Nik\_Wakelin](https://discuss.rubyonrails.org/u/Nik_Wakelin)\
**Post date:** [July 16, 2008, 12:06am UTC](https://discuss.rubyonrails.org/t/association-hash-conditions/31869/6 "2008-07-16T00:06:37Z")

</div>

That does look cool. I'll email you off-list.
