The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
A Rails Feature You Should be Using: with_scope

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
rwdaigle

Posts: 312
Nickname: rwdaigle
Registered: Feb, 2003

Ryan is a passionate ruby developer with a strong Java background.
A Rails Feature You Should be Using: with_scope Posted: Jul 20, 2006 9:23 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by rwdaigle.
Original Post: A Rails Feature You Should be Using: with_scope
Feed Title: Ryan's Scraps
Feed URL: http://feeds.feedburner.com/RyansScraps
Feed Description: Ryan Daigle's various technically inclined rants along w/ the "What's new in Edge Rails" series.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by rwdaigle
Latest Posts From Ryan's Scraps

Advertisement

The with_scope method provided by ActiveRecord has been talked about before (see the resources section below), but I don’t feel like people recognize what a great utility it is. Maybe awareness will increase with more tools that make use of this feature, like the MeantimeFilter by Roman, or just more public conversation about it. Add the following to the latter category:

with_scope lets you bind a block of code operating on an active record model to a particular subset of that model’s collection. For instance, using the standard blog application example, if I have a controller method that performs a series of operations on a single user’s articles I would need to pass in the user id condition on every operation:


def create_avoid_dups

  user_id = current_user.id
  # Find all user's posts
  user_posts = Post.find(:all, :conditions => ["user_id = ?", user_id])

  # Do some logic looking for dups in user_posts
  ...

  # then create new
  @post = Post.create(:body => params[:body], :user_id => user_id)

end

Notice we had to pass in the user_id on both the find and create method. with_scope lets us extract that parameter so the core operations aren’t obscured by excessive parameters:


def create_avoid_dups

  Post.with_scope(:find => {:conditions => "user_id = #{current_user.id}"},
                  :create => {:user_id => current_user.id}) do

    # Find all user's posts
    # No longer need user_id condition since we're in scope
    user_posts = Post.find(:all)

    # Do some logic looking for dups in user_posts
    ...

    # then create new without specifying user_id
    @post = Post.create(:body => params[:body])
    @post.user_id #=> user_id

  end
end

with_scope allowed us to specify conditions of the Post that would apply throughout the course of the block (conditions specified by operation, in this case :find and :create)

Contrived examples such as this one don’t do a great job of showcasing how useful this method is – but imagine never having to specify the user_id in any controller method because it’s been automatically scoped to that user for you. That’s exactly what the previously mentioned meantime filter does.

So don’t be shy. If you find yourself writing code that applies to a known subset of items, scope it with with_scope. All the cool kids are doing it.

Resources

tags: ,

Read: A Rails Feature You Should be Using: with_scope

Topic: Mucking With Unicode for 1.8 Previous Topic   Next Topic Topic: Lots of JRuby Love Today

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use