#5
Mar 14, 2007

Using with_scope

Learn how to use with_scope - a very powerful method which will allow your custom find methods to accept any find options. Just like magic!
Download (5.9 MB, 2:50)
alternative download for iPod & Apple TV (3.9 MB, 2:50)
# models/task.rb
def self.find_incomplete(options = {})
  with_scope :find => options do
    find_all_by_complete(false, :order => 'created_at DESC')
  end
end

RSS Feed for Episode Comments -9 comments

1. bradc Jun 27, 2007 at 12:37

If I specify an :order option when calling find_incomplete, will with_scope override the one that's already defined by default?


2. Ryan Bates Jun 27, 2007 at 15:57

@bradc, good question. There's no way to override the default :order by passing the options. To change this behavior you need to reverse it so the :order statement is in with_scope and the options are passed directly to the find method.


3. Eric Anderson Oct 16, 2007 at 20:00

I typically reverse the custom find methods so that I also have the option of specifying the type of find I want to do (i.e. :first, :all, array of ids, etc.). So my code would look something like:

<code>
<pre>
def self.find_incomplete(*args, &blk)
  with_scope :find => {:conditions => ['complete = ?', false],
    :order => 'created_at DESC'} do
    find *args, &blk
  end
end
</pre>
</code>

The &blk is not really necessary but if helps future-proof the interface.


4. Evgeni Belin Jan 08, 2008 at 14:15

I was having problems using will_paginate with scope_out. Another plugin "has_finder" seems to provide similar functionality only plays nicely with pagination:

http://zargony.com/2007/10/20/paginating-special-queries-with-hasfinder-and-will_paginate/


5. Kristinka Feb 10, 2010 at 12:48

A very good article, I will always come in.


6. Leontinka Feb 11, 2010 at 14:10

Such a good article, caught my sympathy!


7. chanel shoes Jul 23, 2010 at 01:41

you will enjoy real quality and big discount. Free shipping to your door!


9. zhu zhu pets Aug 04, 2010 at 01:43

<A href="http://www.toptoys2trade.com/animal-rubber-bands-c-46/ ">Animal Shaped Rubber Bands</A>
<A href="http://www.toptoys2trade.com/baby-carriers-c-53/ ">Baby Carriers</A>


10. free directory list Aug 11, 2010 at 22:15

I think this is very dangerous


11. p90 workout Aug 12, 2010 at 09:17

I found your blog on Yahoo and I just wanted to say that I think your writing is simply stunning! Thanks again for providing this content for free.


12. uggs online Aug 13, 2010 at 22:10

Article is very nicely written.


13. техасский покер скачать Aug 16, 2010 at 04:40

техас холдем называется префлоп [url=http://www.pokerpublic.net/]правила покера[/url] Двое


14. louis vuitton shoes Aug 26, 2010 at 21:10

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


15. snow boots Aug 31, 2010 at 02:15

 just wanted to say that I think your writing is simply stunning! Thanks again for providing this content for free.


16. louis vuitton sunglasses Sep 01, 2010 at 21:31

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source