# Sorting has\_many association

**URL:** <https://www.ruby-forum.com/t/sorting-has-many-association/88538>\
**Category:** Rails\
**Created:** [March 12, 2007, 1:40pm UTC](https://www.ruby-forum.com/t/sorting-has-many-association/88538 "2007-03-12T13:40:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [March 12, 2007, 1:40pm UTC](https://www.ruby-forum.com/t/sorting-has-many-association/88538/1 "2007-03-12T13:40:25Z")

</div>

i’ve got a specific problem here and I’m not sure how to handle it.

what i need to do is sort a has\_many association in a very specific way.

class Foo \< AR::Base  
has\_many :things  
end

class Thing \< AR::Base  
belongs\_to :foo  
end

class Order \< Thing  
end

class BInCheck \< Thing  
end

class Problem \< Thing  
end

class Special \< Thing  
end

class CycleCount \< Thing  
end

class Notification \< AR::Base

PRIORITIES = [:order, :bin\_check, :problem, :special, :cycle\_count]  
end

now, what i need to do when when a foo goes into an ‘available’ state,  
i need to create a notification for whatever needs foo next, based on  
the notification priorities. the problem is the sorting. what i want  
to do is sort the things for foo according to the  
Notification::PRIORITIES array. so that all i have to do is pass the  
first element in to generate the notification. note, that date also  
plays a part in that if there are 2+ of any type, they are sorted by  
date (:created\_on attribute).

one thing i thought of using was an association extension on Foo

has\_many :things do  
def for\_notification  
…  
end  
end

bug again, i’m still blocked on the sorting.

any ideas?

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [March 12, 2007, 1:49pm UTC](https://www.ruby-forum.com/t/sorting-has-many-association/88538/2 "2007-03-12T13:49:27Z")

</div>

Chris H. wrote:

> end  
> class Special \< Thing  
> now, what i need to do when when a foo goes into an ‘available’ state,  
> has\_many :things do  
> def for\_notification  
> …  
> end  
> end
> 
> bug again, i’m still blocked on the sorting.
> 
> any ideas?

How do you decide which priority to pick? I’m assuming that :order is  
not the  
one that’s always called every time.

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [March 12, 2007, 2:08pm UTC](https://www.ruby-forum.com/t/sorting-has-many-association/88538/3 "2007-03-12T14:08:32Z")

</div>

yes, that’s the priority. per the requirements.

when a ‘foo’ becomes available a notification should be generated  
based on the following priorities, highest to lowest

order, bin check, problem, special, cycle count

if there are multiple of a particular type, the oldest gets the  
notification.

so basically what this means if i have say, 3 order, 1 bin check and 1  
special associated with foo

the oldest order gets the notification

if i have 1 bin check and 2 cycle count, the bin check gets the  
notification.

hence why i’d like to sort based on the priorities (and created\_on  
within each) i listed and then just take the first one and generate a  
priority

something like

Notification.generate\_for foo.things.sorted\_for\_notifications.first

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [March 12, 2007, 2:25pm UTC](https://www.ruby-forum.com/t/sorting-has-many-association/88538/4 "2007-03-12T14:25:35Z")

</div>

Chris H. wrote:

> special associated with foo
> 
> Notification.generate\_for foo.things.sorted\_for\_notifications.first

What’s the column that’s storing the priority in Thing? If you are  
storing the  
priority as a string in Thing the simple thing to is to prefix something  
in  
the front of the priorities that allows you to sort them easily like:

a\_order, b\_bin\_check, etc.

or

1\_order, 2\_bin\_check, etc.

then you can just do :order =\> “priority, created\_at”.

If you are storing it as an integer then you just need to make sure that  
they  
are ordered properly. E.g. order = 1, bin\_check = 2, etc.

–  
Michael W.
