#76
Oct 22, 2007

scope_out

The scope_out plugin will generate several helpful find methods for you automatically! It's the best way to move the find conditions into your model. Watch this episode for details.
Tags: plugins models
Download (8.5 MB, 6:38)
alternative download for iPod & Apple TV (7.5 MB, 6:38)

References

script/plugin install http://scope-out-rails.googlecode.com/svn/trunk/
# models/task.rb
scope_out :incomplete, :conditions => ['complete=?', false], :order => 'name'

# tasks_controller.rb
@tasks = Task.find_incomplete(:all)

# models/project.rb
has_many :tasks, :extend => Task::AssociationMethods

# projects_controller.rb
def show
  @project = Project.find(params[:id])
  @tasks = @project.tasks.find_incomplete(:all)
end

RSS Feed for Episode Comments 18 comments

1. pimpmaster Oct 22, 2007 at 04:39

Woohooo! First Comment!

I'm special :)


2. Teflon Ted Oct 22, 2007 at 04:47

Great stuff, as usual. If Task had two scope outs, say "incomplete" and "ordered by name", how might you combine them when needed? I hope there's a part 2 to scope_out :-) Thanks.


3. pimpmaster Oct 22, 2007 at 05:00

Ted,

You can see how to do that on scope_out's home page

http://code.google.com/p/scope-out-rails/


4. Reza Oct 22, 2007 at 06:23

I think there is a problem with the video file. I can't open it with VLC Player on PC. Could you please check it out? Thank your.


5. BlueBits Oct 22, 2007 at 12:41

I like how you are still actively releasing new podcasts. Most of the other "programming" podcasts I came accross either have irregular updates or just stop updating all together after a relatively short period of time. July was your most active month; if you could repeat that I wouldn't complain!
anyway, fantastic podcasts. Keep up the good work.


6. Zargony Oct 22, 2007 at 12:57

Another great screencast, thanks Ryan!

I've been using scope_out for some time, but unfortunately every now and then, I encountered problems especially when it comes to pagination. Some time ago I wrote up a summary in my blog: http://zargony.com/2007/07/21/paginating-special-queries/

Recently I started to use the HasFinder plugin instead of scope_out. HasFinder works almost like scope_out, but it additionally gives you an easy way to arbitrarily combine scopes, like @project.tasks.incomplete.important (Task has finders :incomplete and :important). Plus it works out of the box with caching and pagination. I posted a quick summary at http://zargony.com/2007/10/20/paginating-special-queries-with-hasfinder-and-will_paginate/

HasFinder can be found at http://www.pivotalblabs.com/articles/2007/09/02/hasfinder-its-now-easier-than-ever-to-create-complex-re-usable-sql-queries


7. Jonzo Oct 23, 2007 at 01:54

Hi there Ryan!

Long time listener first time caller! First things first, you're awesome!

OK second thing... something I've been wondering for a while is whether it's possible to extend scopes from somewhere else (e.g. a controller).

for example, in this episode you have:

scope_out :incomplete, :conditions => ['complete=?', false]

Sometimes I might want to add extra conditions when using this:

Task.find_incomplete(:all, :conditions => "rank = 'urgent'")

Do you know if there is a way to do this? maybe if you pass the conditions as a hash? (is this only available in edge rails?)


8. Ryan Bates Oct 23, 2007 at 14:00

@Reza, hmm, I encoded this one the same way I do the others and it plays okay here. Are you still having a problem?

@Jonzo, you should be able to pass additional conditions in the find exactly how you do in your code. Does it not work?


9. Jonzo Oct 23, 2007 at 18:15

@Ryan, yeah I thought it didn't work when I tried it... (specifically when using the 'conditions' variable twice) Maybe I was doing something else wrong :-O Good to know though, I'll re-visit it, Cheers :-)


10. Jonzo Oct 24, 2007 at 02:46

Retried it and it works! I love it when I look like a crazy person... ;-)


11. Reza Oct 24, 2007 at 02:56

@Ryan, I'll try to download and check it again. But there is something different with this one. The rest are playing OK in vlc player and quicktime alternative, but this one only plays on Apple Quicktime only. By the way, you are one of my personal heroes in the internet. Good job!


12. Erik Oct 24, 2007 at 13:32

I was wondering how this method differs/compares to the one explain here, under "Case Three – Special Queries" (the 3'rd example):

http://www.therailsway.com/2007/3/26/association-proxies-are-your-friend

Thanks again for your casts, they've taught me a lot and I'll be sure to donate.


13. Juha Oct 25, 2007 at 10:32

Nice work, again.

Have you thought about distributing these screencast files with BitTorrent?


14. andy Nov 02, 2007 at 10:50

I'm finding scope_out fails to assert key when using the :group (as in "group by") clause of find method.

Am I the only to notice this?

ArgumentError (Unknown key(s): group):
    /var/lib/gems/1.8/gems/activesupport-1.4.4/lib/active_support/core_ext/hash/keys.rb:48:in `assert_valid_keys'


15. Tobi Feb 05, 2008 at 01:34

I have some problems using scope_out and ferret+acts_as_ferret. When I configure acts_as_ferret and try to start the server I always get an error:

(druby://localhost:9010) /....../vendor/plugins/scope_out/lib/scope_out.rb:70:in `method_missing': undefined method `aaf_configuration' for #<Class:0x251b3bc> (NoMethodError)

Does anyone experience the same problems?


16. joe Mar 13, 2008 at 02:52

I had an related problem but restarting ferret_server did help for me.


17. Marinusha Nov 27, 2009 at 09:17

To Lidya
Places takes less traffic and less eating


18. Lilyusya Feb 01, 2010 at 10:55

Маловато конечно, но и на этом спасибо!


19. job search Mar 01, 2010 at 17:50

yes, it is really very helpful, so thank you!


20. Dona Mar 10, 2010 at 08:51

Методика действенна. Проверено на собственном компе


21. Король Apr 26, 2010 at 02:44

Методика супер. На компе своем проверил.


22. Marianka May 07, 2010 at 05:57

good


23. bathroom vanities Jul 11, 2010 at 00:42

Thanks for the sharing. I think this site is pretty good. Love this site, so I will visit again to check your updates.


24. Louis Vuitton Bum Bag M95347 Jul 16, 2010 at 20:23

I was actually looking for this resource a few weeks back. Thanks for sharing with us your wisdom.This will absolutely going to help me in my projects


25. nike shoes blog Aug 01, 2010 at 23:29

i thought about distributing these screencast files with BitTorrent.


27. logo design Aug 09, 2010 at 23:34

"Stunning! Really good work!
Hard to choose one favorite, but if only one could get my vote it would be Piano Forrest."


28. p90 workout Aug 12, 2010 at 09:16

Yes, thats exactly what I wanted to hear! Great stuff here. The information and the detail were just perfect. I think that your perspective is deep, its just well thought out and really fantastic to see someone who knows how to put these thoughts down so well. Great job on this.


29. buy jordans Aug 16, 2010 at 00:29

Thanks for sharing with us your wisdom.This will absolutely going to help me in my projects. The information and the detail were just perfect. I think that your perspective is deep, its just well thought out and really fantastic to see someone who knows how to put these thoughts down so well.


30. iPhone repair Aug 23, 2010 at 04:44

Master and Servants Act was the culmination of a series of laws designed to regulate relations between employers and employees during the 18th and 19th centuries, although heavily biased on the employers' terms.


31. Promotional merchandise Aug 25, 2010 at 23:55

You can also google cities and find statistics on incomes, race, marital status, employment, schools.


32. louis vuitton shoes Aug 26, 2010 at 21:17

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


33. snow boots Aug 31, 2010 at 00:52

something I've been wondering for a while is whether it's possible to extend scopes from somewhere else (e.g. a controller).


34. louis vuitton sunglasses Sep 01, 2010 at 21:17

Good article! Thank you so much for sharing this post.Your views truly open my mind.

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