#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

17 comments

pimpmaster Oct 22, 2007 at 04:39

Woohooo! First Comment!

I'm special :)


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.


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/


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.


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.


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


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?)


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?


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 :-)


Jonzo Oct 24, 2007 at 02:46

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


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!


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.


Juha Oct 25, 2007 at 10:32

Nice work, again.

Have you thought about distributing these screencast files with BitTorrent?


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'


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?


joe Mar 13, 2008 at 02:52

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


kino May 23, 2008 at 01:51

In the study of time, the Transcendental Deduction (and it remains a mystery why this is true) is what first gives rise to the phenomena.

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(required)

subscribe:
sponsored by:
if you want to help:
required:
Get Quicktime Player