On Thursday, July 12, 2012 12:47:23 AM UTC-7, Joe Van Dyk wrote:
>
> Or, I could put the methods into a Module and extend the dataset object:
>
> module TheModule
> def with_buyable ...
> end
> end
>
> DB[:products].extend(TheModule).with_buyable.with_recent
>
> Does anyone do anything similar?
>
Well, most other people use models, which do something similar. :)
For pure reporting applications, I have used the approach you suggest
(extending datasets with a module). I assign such a dataset to a
constant, similar to how a model class works:
Product = DB[:products].extend(TheModule)
Product.buyable.recent
To speak the original question about combining datasets, the reason Sequel
does not automatically combine two datasets other than via
union/intersect/except is that there is no way to do so in other than the
simplest cases. For example:
DB[:table].where(:a=>1).merge(DB[:table].where(:b=>2))
Could theoretically work by merging the WHERE clauses. However, consider:
DB[:table1].where(:a=>1).merge(DB[:table2].where(:b=>2))
or:
DB[:table].order(:a, :b).merge(DB[:table].order(:b, :a))
Note that combining where clauses for two different datasets is a trivial
operation in Sequel:
class Sequel::Dataset
def merge_where(ds)
if w = ds.opts[:where]
where(w)
else
clone
end
end
end
The reason such a method doesn't exist by default is because it's a bad
idea, not because it is difficult to implement.
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/sequel-talk/-/Jo7uPlnz1RAJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en.