Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
:throw option for AR finders that don't throw RecordNotFound exceptions
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 36 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Tim Lucas  
View profile  
 More options Aug 30 2006, 12:37 am
From: Tim Lucas <t.lu...@toolmantim.com>
Date: Wed, 30 Aug 2006 14:37:52 +1000
Local: Wed, Aug 30 2006 12:37 am
Subject: :throw option for AR finders that don't throw RecordNotFound exceptions
When using ActiveRecord finders that don't by throw a RecordNotFound  
exception I'm finding myself writing code like the following:

   def show
     @tag = Tag.find_by_name(params[:id]) or raise  
ActiveRecord::RecordNotFound
   end

I don't really like the idea of raising a RecordNotFound myself, as,  
at the least, I don't get the exception message that ActiveRecord  
includes if it throws the exception internally ("Couldn't find Tag  
with ID=9999")... and it just feels... wrong.

Wouldn't it be better to have a finder option to specify you want an  
exception thrown if there's no record found?

   def show
     @tag = Tag.find_by_name(params[:id], :throw_not_found => true)
   end

Other possible APIs:

:throw_if_not_found => true
:throw_if_nil => true
:throw_when_not_found => true
:throw_when_nil => true
:throw_on_not_found => true
:throw_on_nil => true
:not_found => :throw

Thoughts?

-- tim lucas


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jamis Buck  
View profile  
 More options Aug 30 2006, 12:43 am
From: Jamis Buck <ja...@37signals.com>
Date: Tue, 29 Aug 2006 22:43:27 -0600
Local: Wed, Aug 30 2006 12:43 am
Subject: Re: [Rails-core] :throw option for AR finders that don't throw RecordNotFound exceptions
Tim,

I like the idea. I'd suggest :must_exist => true as another possible API.

- Jamis


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kevin Clark  
View profile  
 More options Aug 30 2006, 12:44 am
From: "Kevin Clark" <kevin.cl...@gmail.com>
Date: Tue, 29 Aug 2006 21:44:53 -0700
Local: Wed, Aug 30 2006 12:44 am
Subject: Re: [Rails-core] :throw option for AR finders that don't throw RecordNotFound exceptions
Isn't find_by_name handled in method missing? Could we add a check for
a bang at the end so:

@tag.find_by_name # => doesn't throw if nil
@tag.find_by_name! # => throws with nil

PDI

Kev

On 8/29/06, Tim Lucas <t.lu...@toolmantim.com> wrote:

--
Kevin Clark
http://glu.ttono.us

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Koziarski  
View profile  
 More options Aug 30 2006, 12:55 am
From: "Michael Koziarski" <mich...@koziarski.com>
Date: Wed, 30 Aug 2006 16:55:45 +1200
Local: Wed, Aug 30 2006 12:55 am
Subject: Re: [Rails-core] Re: :throw option for AR finders that don't throw RecordNotFound exceptions

> Isn't find_by_name handled in method missing? Could we add a check for
> a bang at the end so:

> @tag.find_by_name # => doesn't throw if nil
> @tag.find_by_name! # => throws with nil

> PDI

I prefer the bang approach to the :some_hash_key thing.  In the past
I've wanted:

Whatever.find(:one, ...) which is like find first but throws for <> 1
rows....  But this seems like a nice middle ground.

--
Cheers

Koz


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Lucas  
View profile  
 More options Aug 30 2006, 12:59 am
From: Tim Lucas <t.lu...@toolmantim.com>
Date: Wed, 30 Aug 2006 14:59:48 +1000
Local: Wed, Aug 30 2006 12:59 am
Subject: Re: [Rails-core] Re: :throw option for AR finders that don't throw RecordNotFound exceptions
On 30/08/2006, at 2:43 PM, Jamis Buck wrote:

> I like the idea. I'd suggest :must_exist => true as another  
> possible API.

> - Jamis

I knew somebody would have a better suggestion :)

-- tim


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeremy Kemper  
View profile  
 More options Aug 30 2006, 1:04 am
From: "Jeremy Kemper" <jer...@bitsweat.net>
Date: Tue, 29 Aug 2006 22:04:24 -0700
Local: Wed, Aug 30 2006 1:04 am
Subject: Re: [Rails-core] :throw option for AR finders that don't throw RecordNotFound exceptions

On 8/29/06, Tim Lucas <t.lu...@toolmantim.com> wrote:

> Wouldn't it be better to have a finder option to specify you want an
> exception thrown if there's no record found?

I'd prefer a bang! method.  I think it's a good indication that there should
be separate 'loading' and 'finding' API -- passing ever more options to find
is getting weary.

Loading a unique record from the database is semantically different than
finding a record that meets some criteria and it could be treated as such.

jeremy


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Lucas  
View profile  
 More options Aug 30 2006, 1:06 am
From: Tim Lucas <t.lu...@toolmantim.com>
Date: Wed, 30 Aug 2006 15:06:07 +1000
Local: Wed, Aug 30 2006 1:06 am
Subject: Re: [Rails-core] Re: :throw option for AR finders that don't throw RecordNotFound exceptions
On 30/08/2006, at 2:44 PM, Kevin Clark wrote:

> Isn't find_by_name handled in method missing? Could we add a check for
> a bang at the end so:

> @tag.find_by_name # => doesn't throw if nil
> @tag.find_by_name! # => throws with nil

You'd then also have to add a find! and document this in both the  
dynamic finders section of the docs as well as the find! method.

Seems simpler to add it as an option, but i do like the bang. hrmm...

Also, with a namespace of 1, could the bang possibly be needed for  
another purpose in the future? Nothing really comes to mind.

-- tim


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kevin Clark  
View profile  
 More options Aug 30 2006, 1:43 am
From: "Kevin Clark" <kevin.cl...@gmail.com>
Date: Tue, 29 Aug 2006 22:43:32 -0700
Local: Wed, Aug 30 2006 1:43 am
Subject: Re: [Rails-core] Re: :throw option for AR finders that don't throw RecordNotFound exceptions

> You'd then also have to add a find! and document this in both the
> dynamic finders section of the docs as well as the find! method.

def find!(*args)
  val = find(*args)
  raise blah if val.nil? or (val == [])
  return val
end

Or something like that. I wrote it in gmail so it isn't tested.

--
Kevin Clark
http://glu.ttono.us


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jarkko Laine  
View profile  
 More options Aug 30 2006, 2:03 am
From: Jarkko Laine <jar...@jlaine.net>
Date: Wed, 30 Aug 2006 09:03:45 +0300
Local: Wed, Aug 30 2006 2:03 am
Subject: Re: [Rails-core] Re: :throw option for AR finders that don't throw RecordNotFound exceptions

On 30.8.2006, at 8.43, Kevin Clark wrote:

>> You'd then also have to add a find! and document this in both the
>> dynamic finders section of the docs as well as the find! method.

> def find!(*args)
>   val = find(*args)
>   raise blah if val.nil? or (val == [])

val.blank?, man ;-)

--
Jarkko Laine
http://jlaine.net
http://odesign.fi

  smime.p7s
3K Download

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kevin Clark  
View profile  
 More options Aug 30 2006, 2:24 am
From: "Kevin Clark" <kevin.cl...@gmail.com>
Date: Tue, 29 Aug 2006 23:24:07 -0700
Local: Wed, Aug 30 2006 2:24 am
Subject: Re: [Rails-core] Re: :throw option for AR finders that don't throw RecordNotFound exceptions
Does .blank? handle [] as well as "" and nil? I was going to use
.empty? but then I'd have to check that the method existed.

On 8/29/06, Jarkko Laine <jar...@jlaine.net> wrote:

--
Kevin Clark
http://glu.ttono.us

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Kaspick  
View profile  
 More options Aug 30 2006, 2:39 am
From: "Andrew Kaspick" <akasp...@gmail.com>
Date: Wed, 30 Aug 2006 01:39:08 -0500
Local: Wed, Aug 30 2006 2:39 am
Subject: Re: [Rails-core] Re: :throw option for AR finders that don't throw RecordNotFound exceptions
blank? handles []

class Array #:nodoc:
  alias_method :blank?, :empty?
end

On 8/30/06, Kevin Clark <kevin.cl...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Kaspick  
View profile  
 More options Aug 30 2006, 2:41 am
From: "Andrew Kaspick" <akasp...@gmail.com>
Date: Wed, 30 Aug 2006 01:41:10 -0500
Local: Wed, Aug 30 2006 2:41 am
Subject: Re: [Rails-core] Re: :throw option for AR finders that don't throw RecordNotFound exceptions
From the source comments actually, so {} and "   " too...

# "", "   ", nil, [], and {} are blank

On 8/30/06, Andrew Kaspick <akasp...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jarkko Laine  
View profile  
 More options Aug 30 2006, 2:43 am
From: Jarkko Laine <jar...@jlaine.net>
Date: Wed, 30 Aug 2006 09:43:41 +0300
Local: Wed, Aug 30 2006 2:43 am
Subject: Re: [Rails-core] Re: :throw option for AR finders that don't throw RecordNotFound exceptions

On 30.8.2006, at 9.24, Kevin Clark wrote:

> Does .blank? handle [] as well as "" and nil? I was going to use
> .empty? but then I'd have to check that the method existed.

Oh yes, it's ingeniuos. See http://dev.rubyonrails.org/browser/trunk/
activesupport/lib/active_support/core_ext/blank.rb

//jarkko

--
Jarkko Laine
http://jlaine.net
http://odesign.fi

  smime.p7s
3K Download

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kevin Clark  
View profile  
 More options Aug 30 2006, 2:50 am
From: "Kevin Clark" <kevin.cl...@gmail.com>
Date: Tue, 29 Aug 2006 23:50:00 -0700
Subject: Re: [Rails-core] Re: :throw option for AR finders that don't throw RecordNotFound exceptions
wonderous. Either way, easy to write find!

On 8/29/06, Jarkko Laine <jar...@jlaine.net> wrote:

--
Kevin Clark
http://glu.ttono.us

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Koziarski  
View profile  
 More options Aug 30 2006, 2:52 am
From: "Michael Koziarski" <mich...@koziarski.com>
Date: Wed, 30 Aug 2006 18:52:03 +1200
Local: Wed, Aug 30 2006 2:52 am
Subject: Re: [Rails-core] Re: :throw option for AR finders that don't throw RecordNotFound exceptions
On 8/30/06, Andrew Kaspick <akasp...@gmail.com> wrote:

> blank? handles []

> class Array #:nodoc:
>   alias_method :blank?, :empty?
> end

>> [].blank?
=> true
>> nil.blank?
=> true
>> "".blank?

=> true

--
Cheers

Koz


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Lucas  
View profile  
 More options Aug 30 2006, 3:17 am
From: Tim Lucas <t.lu...@toolmantim.com>
Date: Wed, 30 Aug 2006 17:17:22 +1000
Local: Wed, Aug 30 2006 3:17 am
Subject: Re: [Rails-core] Re: :throw option for AR finders that don't throw RecordNotFound exceptions
On 30/08/2006, at 4:50 PM, Kevin Clark wrote:

> wonderous. Either way, easy to write find!

module ActiveRecord
   class Base
     def find!(*args)
       records = find(args)
       raise RecordNotFound, "Couldn't find #{name} without an ID" if  
records.blank?
       records
     end
   end
end

works a treat. Adding ! to the method_missing finders might be a bit  
trickier.

I've filed a ticket seeing as Aksimet seems to be in a good mood at  
the moment:
http://dev.rubyonrails.org/ticket/5974

-- tim


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeremy Kemper  
View profile  
 More options Aug 30 2006, 3:40 am
From: "Jeremy Kemper" <jer...@bitsweat.net>
Date: Wed, 30 Aug 2006 00:40:14 -0700
Local: Wed, Aug 30 2006 3:40 am
Subject: Re: [Rails-core] Re: :throw option for AR finders that don't throw RecordNotFound exceptions

On 8/30/06, Tim Lucas <t.lu...@toolmantim.com> wrote:

> I've filed a ticket seeing as Aksimet seems to be in a good mood at
> the moment:

Akismet is disabled due to its patch gobbling.

jeremy


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Wanstrath  
View profile  
 More options Aug 30 2006, 3:07 pm
From: Chris Wanstrath <ch...@ozmm.org>
Date: Wed, 30 Aug 2006 12:07:08 -0700
Local: Wed, Aug 30 2006 3:07 pm
Subject: [Rails-core] Re: :throw option for AR finders that don't throw RecordNotFound exceptions
So, if this `find!' bidness gets introduced it means an exception can  
get raised a) on record not found via find! / find_by_*!  or b) on  
record not found via find(id).  Raised on bangs or on non-bang.

I know the non-bang is a very specific circumstance, but does this  
bother anyone else?  I suppose one way of looking at this is that the  
bang methods are just sugar for existing methods, none of which  
change.  But it sure would be nice to just say "methods with a bang  
raise an exception on blank, methods without do not."

I can deal with it; I love that this change is being discussed.  But  
won't someone think of the children?

--
Chris Wanstrath
http://errtheblog.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven A Bristol  
View profile  
 More options Aug 30 2006, 3:18 pm
From: "Steven A Bristol" <stevenbris...@gmail.com>
Date: Wed, 30 Aug 2006 15:18:17 -0400
Local: Wed, Aug 30 2006 3:18 pm
Subject: Re: [Rails-core] :throw option for AR finders that don't throw RecordNotFound exceptions

+1 to Chris.

I often override find(id) to return nil instead of raising. I would love to
see find(*) return ||= nil and find!(*) return ||= raise.

Steven A Bristol
http://stevenbristol.blogspot.com

On 8/30/06, Chris Wanstrath <ch...@ozmm.org> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rick Olson  
View profile  
 More options Aug 30 2006, 3:30 pm
From: "Rick Olson" <technowee...@gmail.com>
Date: Wed, 30 Aug 2006 14:30:18 -0500
Local: Wed, Aug 30 2006 3:30 pm
Subject: Re: [Rails-core] Re: :throw option for AR finders that don't throw RecordNotFound exceptions
-1

I count on the fact that find(id) raises an exception.  If I'm looking
up a model by id and it's not there, there's usually a problem.  The
raises RecordNotFound exception is a convenient hook to render a 404
page.

If you really want it to return nil, why not use find_by_id ?

--
Rick Olson
http://weblog.techno-weenie.net
http://mephistoblog.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeremy Kemper  
View profile  
 More options Aug 30 2006, 3:30 pm
From: "Jeremy Kemper" <jer...@bitsweat.net>
Date: Wed, 30 Aug 2006 12:30:59 -0700
Local: Wed, Aug 30 2006 3:30 pm
Subject: Re: [Rails-core] :throw option for AR finders that don't throw RecordNotFound exceptions

On 8/30/06, Chris Wanstrath <ch...@ozmm.org> wrote:

> So, if this `find!' bidness gets introduced it means an exception can
> get raised a) on record not found via find! / find_by_*!  or b) on
> record not found via find(id).  Raised on bangs or on non-bang.

> I know the non-bang is a very specific circumstance, but does this
> bother anyone else?  I suppose one way of looking at this is that the
> bang methods are just sugar for existing methods, none of which
> change.  But it sure would be nice to just say "methods with a bang
> raise an exception on blank, methods without do not."

> I can deal with it; I love that this change is being discussed.  But
> won't someone think of the children?

I remarked earlier that I think it's a good indication that there should be
separate 'loading' and 'finding' API.  Find is way overloaded, though that
can be valuable trait, as with Ruby's relatively small set of builtin
collection classes each serving many needs.

Perhaps:
  Person.load(1)
  Person.load_by_name('bob')
meaning "give me the record that I've uniquely identified for you" versus
  Person.find_by_name('bob')
meaning "look for records matching the criteria I've given and return the
first one"

jeremy


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Zack Chandler  
View profile  
 More options Aug 30 2006, 3:39 pm
From: "Zack Chandler" <zackchand...@gmail.com>
Date: Wed, 30 Aug 2006 12:39:58 -0700
Local: Wed, Aug 30 2006 3:39 pm
Subject: Re: [Rails-core] Re: :throw option for AR finders that don't throw RecordNotFound exceptions
On 8/30/06, Rick Olson <technowee...@gmail.com> wrote:

-1

I agree with Rick here.  I count on find(id) throwing an exception.  I
use find_by_id(id) if I want it the other way.

--
Zack Chandler
http://depixelate.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jamie van Dyke  
View profile  
 More options Aug 30 2006, 3:49 pm
From: Jamie van Dyke <ja...@fearoffish.co.uk>
Date: Wed, 30 Aug 2006 20:49:10 +0100
Local: Wed, Aug 30 2006 3:49 pm
Subject: Re: [Rails-core] Re: :throw option for AR finders that don't throw RecordNotFound exceptions

-1

I use find_by_id if I want nil, there's no need to change behaviour  
if it can be achieved using a different method.

Cheers,
Jamie van Dyke
Fear of Fish
http://www.fearoffish.co.uk

On 30 Aug 2006, at 20:30, Rick Olson wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Wanstrath  
View profile  
 More options Aug 30 2006, 4:02 pm
From: Chris Wanstrath <ch...@ozmm.org>
Date: Wed, 30 Aug 2006 13:02:06 -0700
Local: Wed, Aug 30 2006 4:02 pm
Subject: Re: [Rails-core] Re: :throw option for AR finders that don't throw RecordNotFound exceptions
On Aug 30, 2006, at 12:39 PM, Zack Chandler wrote:

> On 8/30/06, Rick Olson <technowee...@gmail.com> wrote:
>> If you really want it to return nil, why not use find_by_id ?

> I agree with Rick here.  I count on find(id) throwing an exception.  I
> use find_by_id(id) if I want it the other way.

I agree with you guys, all I'm saying is that if we go ahead with  
find! we will have a `find' which does about a million different  
things.  It sometimes raises exceptions if there's a bang and  
sometimes raises exceptions if there's not a bang. Complexity?

I like what Jeremy is saying about load vs find.  I think that's what  
I'm reaching for.  When you find(13) you're asking Rails to load a  
specific record.  When you find_by_id(13) you're like "hey give me  
this if you can, if not that's cool too."  Maybe that is a route to  
discuss.

--
Chris Wanstrath
http://errtheblog.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
evn  
View profile  
 More options Aug 30 2006, 4:30 pm
From: "evn" <ewea...@gmail.com>
Date: Wed, 30 Aug 2006 20:30:40 -0000
Local: Wed, Aug 30 2006 4:30 pm
Subject: Re: :throw option for AR finders that don't throw RecordNotFound exceptions
I think maybe the issue is that the behavior of find_by_id(id) and
find(id) is un-least-surprising.

I would be very happy if bang-find always threw an exception on empty,
no matter the parameters or dynamic inlined fields in the method name,
and no-bang-find never did. We already use ! for that purpose on create
and save as had been noted so I don't think it would make the API less
Ruby-esque.

Evan


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 36   Newer >
« Back to Discussions « Newer topic     Older topic »

Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google