On Saturday, July 7, 2012 3:43:48 PM UTC-6, Jeremy Evans wrote:
>
> 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.
>
>
I was hoping to not use Sequel::Model.  :)
 

> 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'"))
>
>
I don't think that sort of intersection would be nearly as efficient as 
combining the where clauses.
 

> 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'")
>

Right, so what I was looking for is a way to combine these where clauses.

Maybe I want to do something sorta like:

def Decorate ds                                                             
                        
  def ds.with_recent                                                       
                         
    where("start_sale_date < now() - interval '1 month'")                   
                        
  end                                                                       
                        
                                                                            
                        
  def ds.with_buyable                                                       
                        
    where(:buyable => true)                                                 
                        
  end                                                                       
                        
  ds                                                                       
                         
end                                                                         
                        
                                                                            
                        
puts Decorate(DB[:product_listings]).with_buyable.with_recent.sql

Thoughts?



>

-- 
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/-/E20tEfieJlAJ.
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