From 293875457bc5b0fccbf3e64bcd275cdac252f98c Mon Sep 17 00:00:00 2001 From: wangjohn Date: Mon, 11 Feb 2013 23:23:01 -0500 Subject: Created an unscope method for removing relations from a chain of relations. Specific where values can be unscoped, and the unscope method still works when relations are merged or combined. --- guides/source/active_record_querying.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'guides') diff --git a/guides/source/active_record_querying.md b/guides/source/active_record_querying.md index 5d82541da9..0d0813c56a 100644 --- a/guides/source/active_record_querying.md +++ b/guides/source/active_record_querying.md @@ -692,6 +692,27 @@ The SQL that would be executed: SELECT * FROM posts WHERE id > 10 LIMIT 20 ``` +### `unscope` + +The `except` method does not work when the relation is merged. For example: + +```ruby +Post.comments.except(:order) +``` + +will still have an order if the order comes from a default scope on Comment. In order to remove all ordering, even from relations which are merged in, use unscope as follows: + +```ruby +Post.order('id DESC').limit(20).unscope(:order) = Post.limit(20) +Post.order('id DESC').limit(20).unscope(:order, :limit) = Post.all +``` + +You can additionally unscope specific where clauses. For example: + +```ruby +Post.where(:id => 10).limit(1).unscope(:where => :id, :limit).order('id DESC') = Post.order('id DESC') +``` + ### `only` You can also override conditions using the `only` method. For example: -- cgit v1.2.3