On Saturday, July 7, 2012 2:09:43 PM UTC-7, Joe Van Dyk wrote:
>
> Given the following:
>
> buyable = DB[:product_listings].where(:buyable => true)                   
>                           
> recent  = DB[:product_listings].where("start_sale_date > now() - interval 
> '1 day'")
>
> What's the best way to find all recent buyable products?  (Ideally, 
> without using Sequel::Model)
>
> In AR, I'd do:
>
> class ProductListing
>   scope :buyable, where(:buyable => true)
>   scope :recent, where("start_sale_date > now() - interval '1 day'")
> end
> ProductListing.buyable.recent
>

You probably want to use subset:

class ProductListing
  subset :buyable, :buyable => true
  subset :recent, "start_sale_date > now() - interval '1 day'"
end

or for more general things that aren't simple where conditions, either 
def_dataset_method:

class ProductListing
  def_dataset_method(:buyable){where(:buyable => true)}
  def_dataset_method(:recent){where("start_sale_date > now() - interval '1 
day'")}
end

or dataset_module:

class ProductListing
  dataset_module do
    def buyable; where(:buyable => true) end
    def recent; where("start_sale_date > now() - interval '1 day'") end
  end
end

All of those are for Sequel::Model though.

If you really want to get the intersection of products that are both 
buyable and recent taking the two separate datasets as input, you want an 
SQL INTERSECT query:

  DB[:product_listings].where(:buyable => 
true).intersect(DB[:product_listings].where("start_sale_date > now() - 
interval '1 day'"))

However, it's rare to actually do that.  In most cases you'd just add the 
appropriate buyable and recent filter conditions to the same dataset, where 
they would be ANDed together in the where clause:

  DB[:product_listings].where(:buyable => true).where("start_sale_date > 
now() - interval '1 day'")

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/-/2gAKcGBXXHAJ.
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.

Reply via email to