sorting has_many association

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?

Chris Hall wrote:

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?

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

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

Chris Hall wrote:

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

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.