Hi Jeremy, I thought I got your idea, but I did not. Sorry for bothering you again.
>do_something = event_ds.where(:event_type=>'do_something').max(:created_time) If there is any `event` with `event_type` is `do_something`, do_something will always be not nil. Thus `did_something` will be always true. For example: Lets say I have 2 `model`(s). First one have `event` with `event_type` is `do_something`. Second one does not have any event. With your code, I will receive dataset in which both `did_something` for 2 `model` are `true`. My expectation is to get back the dataset in which `did_something` for first `model` is `true` and second one is `false` Thanks, Bang > On 27 Apr 2016, at 7:00 PM, Jeremy Evans <[email protected]> wrote: > > > > 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] > <mailto:[email protected]>. > To post to this group, send email to [email protected] > <mailto:[email protected]>. > Visit this group at https://groups.google.com/group/sequel-talk > <https://groups.google.com/group/sequel-talk>. > For more options, visit https://groups.google.com/d/optout > <https://groups.google.com/d/optout>. -- 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.
