diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2013-03-03 19:47:48 -0800 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2013-03-03 19:47:48 -0800 |
commit | 1909808171d8bd9e8dcff0dfd652f05557ce6194 (patch) | |
tree | 45aa7a530057061423e41cb6e9a605501bfdf6bf /guides/source/active_record_querying.md | |
parent | d3fa0259696ff20b77f9a2dfd206c9ab8627a669 (diff) | |
parent | 293875457bc5b0fccbf3e64bcd275cdac252f98c (diff) | |
download | rails-1909808171d8bd9e8dcff0dfd652f05557ce6194.tar.gz rails-1909808171d8bd9e8dcff0dfd652f05557ce6194.tar.bz2 rails-1909808171d8bd9e8dcff0dfd652f05557ce6194.zip |
Merge pull request #9200 from wangjohn/unscoping_activerecord_merging
Introduce relation #unscope
Diffstat (limited to 'guides/source/active_record_querying.md')
-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: |