Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

add first_bang to AR

#6613

Before scopes, controller methods did this:

@user = User.find(params[:id])

Now with scopes you can do

@user = User.where(:id => params[:id]).first

However that doesn't raise an exception when the record is not found. With this patch, you can do this:

@user = User.where(:id => params[:id]).first!

And that raises the RecordNotFound exception, as you'd expect.

Reported by Josh Susser · March 24th, 2011 @ 05:03 AM

State: committed
Milestone: 3.1
Assigned to: Andrew White Andrew White
Importance: Low

Activity

  1. Andrew White
    Andrew White
    • State changed from new to open
    • Milestone set to 3.1
    • Assigned user changed from Aaron Patterson to Andrew White
    • Importance changed from to Low

    Josh, can you add last! as well to the patch and I'll apply it

    March 24th, 2011 @ 11:17 AM

  2. Josh Susser
    Josh Susser

    OK, added last! too. (Aaron, you were right!)

    March 24th, 2011 @ 04:33 PM

  3. Josh Susser
    Josh Susser

    By the way, the simplistic example given in the original ticket above isn't the motivating use case. That would be using an association or scope to find a record.

    def show
      @latest_update = current_user.updates.recent.first!
    end
    

    March 24th, 2011 @ 04:45 PM

  4. Aaron Patterson
    Aaron Patterson
    • State changed from open to committed

    Applied and pushed. Thanks.

    March 24th, 2011 @ 04:55 PM

  5. Josh Susser
    Josh Susser

    Ugh, typo in comments. How lame is that? Here's the fix.

    March 25th, 2011 @ 04:46 PM