diff options
author | wangjohn <wangjohn@mit.edu> | 2013-02-11 23:23:01 -0500 |
---|---|---|
committer | wangjohn <wangjohn@mit.edu> | 2013-03-03 20:42:01 -0500 |
commit | 293875457bc5b0fccbf3e64bcd275cdac252f98c (patch) | |
tree | 5f8385454788673ac00004cd9c494b5df02d3cfc /guides/source | |
parent | 9ee6f3cc8ef1cd50648ec2882803943d3bd1f24a (diff) | |
download | rails-293875457bc5b0fccbf3e64bcd275cdac252f98c.tar.gz rails-293875457bc5b0fccbf3e64bcd275cdac252f98c.tar.bz2 rails-293875457bc5b0fccbf3e64bcd275cdac252f98c.zip |
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.
Diffstat (limited to 'guides/source')
-rw-r--r-- | guides/source/active_record_querying.md | 21 |
1 files changed, 21 insertions, 0 deletions
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: |