You can do that inside a .try{} block too, same idea: 

Order.try{|query| 
        if x==1
          query
        else
          query.unscope(:where)
        } 






> On Dec 29, 2014, at 4:30 AM, Frederick Cheung <[email protected]> 
> wrote:
> 
> 
> 
> On Monday, December 29, 2014 1:16:40 AM UTC, Josh wrote:
> I'd like to remove all existing constraints from an ActiveRecord::Relation 
> and leave the rest in-tact.
> 
> I am not looking for #unscoped since I would like to keep any joins/order 
> clauses around.
> 
> Additionally, I would like to re-use the constraints that were removed in 
> another query, so the ability to call #to_sql on them would be a very nice 
> bonus.
> 
> 
> 
> Sounds like you are looking for 
> 
>  some_scope.unscope(:where) 
> 
> 
> Fred
> 
> 
> This is the best I've been able to hack together:
> 
> Order.where(id: 1).where(id: 2).arel.constraints[0].to_sql
> 
>  => "`orders`.`id` = 1 AND `orders`.`id` = 2"
> 
> ...but that just seems wrong.  As far as I can tell #constraints always has 
> one item in it, but I don't know that will always be true.
> 
> 
> 
> Thanks!
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: 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]>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/rubyonrails-talk/1804d715-ebbc-48c9-892b-d73f72b6f1f7%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/rubyonrails-talk/1804d715-ebbc-48c9-892b-d73f72b6f1f7%40googlegroups.com?utm_medium=email&utm_source=footer>.
> 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 "Ruby 
on Rails: 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].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/C6B2FDF3-460E-45D8-8534-C3BE16BB7C8C%40datatravels.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to