I've got two models like this:

class Order < Sequel::Model
  has_many :order_states
end


class OrderState < Sequel::Model

  set_dataset dataset.order(:created_at).reverse
  many_to_one :order
end


An Order can be `active` or `inactive`, users can toggle this. I've added 
an after save hook that creates a new order state if the state was just 
changed, like this:

def after_save
  super
  add_order_state(state: state) if order_states.last.try(:state) != state
end

This all works great in terms of recording the state changes on the order 
over time.

I need to write a dataset_module method `active_at(time)` that can tell me 
if the service order was active at a particular time. The part that is 
tripping me up is, I need to query against the joined `order_states` table, 
then for each `order_state` check the last order state prior to the given 
time and see then select the order if that order_state was active.

If I was looking at a single record I might do something like:

order.order_states.order(:created_at).reverse.where{ created_at < time }.
last.state == "active"

I haven't been able to think of how to write that as a dataset query method 
though. How would you do that?

Thanks!
Andrew

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to