# Conditional order field in has\_many

**URL:** <https://www.ruby-forum.com/t/conditional-order-field-in-has-many/184006>\
**Category:** Rails\
**Created:** [February 23, 2010, 8:17pm UTC](https://www.ruby-forum.com/t/conditional-order-field-in-has-many/184006 "2010-02-23T20:17:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [February 23, 2010, 8:17pm UTC](https://www.ruby-forum.com/t/conditional-order-field-in-has-many/184006/1 "2010-02-23T20:17:08Z")

</div>

I’d like the change the order field in a has\_many relationship based on  
the value of one of my other fields. Is this possible?

In this case my parent model is Quiz and the child is QuizQuestion. If  
a quiz’s ‘style’ attribute is set to ‘sequential’ i’d like to get  
@quiz.questions back in order of quiz\_questions.position, otherwise i’d  
like to get them back in order of quiz\_questions.low\_band.

So, something like the following (i know this doesn’t work because self  
is the class here)

has\_many :quiz\_questions, :order =\> “#{self[:style] == sequential ?  
‘position’ : ‘low\_band’}”

can i use a proc to set the order for example?

thanks, max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [February 23, 2010, 8:27pm UTC](https://www.ruby-forum.com/t/conditional-order-field-in-has-many/184006/2 "2010-02-23T20:27:23Z")

</div>

On Feb 23, 7:17 pm, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> has\_many :quiz\_questions, :order =\> “#{self[:style] == sequential ?  
> ‘position’ : ‘low\_band’}”

The order clause is an sql fragment, so you can’t do quite what you’ve  
written.  
You can of course write an if statement in sql (although you would  
have to also join the parent table), and order by something like  
IF(foo = ‘bar’, position, low\_band)

This wouldn’t end up as a very efficient query though and feels quite  
nasty.

Fred

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [February 23, 2010, 8:43pm UTC](https://www.ruby-forum.com/t/conditional-order-field-in-has-many/184006/3 "2010-02-23T20:43:15Z")

</div>

Frederick C. wrote:

> On Feb 23, 7:17ï¿½pm, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > has\_many :quiz\_questions, :order =\> “#{self[:style] == sequential ?  
> > ‘position’ : ‘low\_band’}”
> 
> The order clause is an sql fragment, so you can’t do quite what you’ve  
> written.  
> You can of course write an if statement in sql (although you would  
> have to also join the parent table), and order by something like  
> IF(foo = ‘bar’, position, low\_band)
> 
> This wouldn’t end up as a very efficient query though and feels quite  
> nasty.
> 
> Fred

yeah, i’m inclined to agree re the nasty. It does work though: in case  
anyone else stumbles across this, i have

has\_many :quiz\_questions, :dependent =\> :destroy, :include =\> [:quiz],  
:order =\> “IF(quizzes.style = ‘sequential’, position, low\_band)”

The extra include is the dodgy bit isn’t it. I’ll give it a go anyway.

Thanks!
