On Wednesday, April 27, 2016 at 7:55:23 AM UTC+2, The Bang Nguyen wrote:
>
> Hi all,
>
> I have 2 models let's say `Model` and `Event`.
> `Model` has `one_to_many` association to `Event`.
>
> `Event` has column `event_type` which indicates the event happened on a 
> `Model`.
> `event_type` could be `do_something` or `undo_something` or 
> `some_others_action`.
>
> I would like to return a `dataset` in which:
> - if there is no `do_something` or `undo_something` event
>       -- return all columns in `Model` plus `did_something` column as 
> `false`.
> - if there is `do_something` and no `undo_something` event
>       -- return all columns in `Model` plus `did_something` column as 
> `true`.
> - if there is `do_something` and `undo_something` event. And 
> `created_time` of last `do_something` is greater than `created_time` of 
> last `undo_something`
>       -- return all columns in `Model` plus `did_something` column as 
> `true`.
> - if there is `do_something` and `undo_something` event. And 
> `created_time` of last `do_something` is less than `created_time` of last 
> `undo_something`
>       -- return all columns in `Model` plus `did_something` column as 
> `false`.
>

I think something like this may work (completely untested):

Model.dataset_module do
  def with_did_something
    event_ds = join(:events, :model_id=>:id)
    do_something = 
event_ds.where(:event_type=>'do_something').max(:created_time)
    undo_something = 
event_ds.where(:event_type=>'undo_something').max(:created_time)
    did_something = do_something && (!undo_something || undo_something < 
do_something)
    ds = select_all(:model_table).select_append(Sequel.as(did_something, 
:did_something))
  end
end

Thanks,
Jeremy

-- 
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 https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to