On Jan 4, 3:45 pm, rohit <[email protected]> wrote:
> I am trying to DRY up some Sequel code and re-use common SQL
> expressions across different models. Ran into the following issue:
>
> estimated_traffic_cost = lambda{(:a__avg_cpc *
> count(:distinct, :visit_id){})}
> gross_profit = lambda{sum(:commission) - estimated_traffic_cost}
> DB[:a].select(&gross_profit)
>
> Sequel::Error: can't express #<Proc:0x03d7a8f4@(irb):119> as a SQL
> literal
>
> This works:
> gross_profit = lambda{sum(:commission) -
> Sequel.virtual_row(&estimated_traffic_cost)}
>
> Is there a better solution?
> Maybe if Sequel encounters a proc in an expression it could call
> Sequel.virtual_row on it?

  class Proc
    def sql_literal(ds)
      ds.literal(Sequel.virtual_row(&self))
    end
  end

Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
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