If find myself doing something like this a lot: Model.find(some_criteria).field which throws an exception if nothing is found. I am looking for a better way of doing something like this: if Model.find(some_criteria) Model.find(some_criteria).field else Model.new end How are other people handling situations like this? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
hi, try x=Model.find(some_criteria).field || Model.new in case you dont require Model.new can use "" as message for nothing ~gaurav --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
gaurav bagga wrote:> hi, > > try > > x=Model.find(some_criteria).field || Model.new > > in case you dont require Model.new can use "" as message for nothing > > ~gauravThat doesn''t work for me. I get a ActiveRecord::RecordNotFound error. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
hi,
in my previous reply I dint think of the exception hence :(
so apologies
i tried something
begin
Model.find(criteria)
rescue
""
end
other thing i tried was
Model.find_by_sql("select * from models where id = 0")
gave me empty array if entry was not there
else array with instance of model matching criteria.
hope this helps
regards
gaurav
On 1/3/07, Ryan
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:>
>
> gaurav bagga wrote:
> > hi,
> >
> > try
> >
> > x=Model.find(some_criteria).field || Model.new
> >
> > in case you dont require Model.new can use "" as message for
nothing
> >
> > ~gaurav
>
> That doesn''t work for me. I get a ActiveRecord::RecordNotFound
error.
>
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On 1/3/07, Ryan <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > If find myself doing something like this a lot: > > Model.find(some_criteria).field > > which throws an exception if nothing is found. I am looking for a > better way of doing something like this: > > if Model.find(some_criteria) > Model.find(some_criteria).field > else > Model.new > end > > How are other people handling situations like this?if record = Record.find_by_id 1 record.foo end -- Rick Olson http://weblog.techno-weenie.net http://mephistoblog.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
gaurav bagga wrote:> hi, > > in my previous reply I dint think of the exception hence :( > so apologies > > i tried something > > begin > Model.find(criteria) > rescue > "" > end > > other thing i tried was > > Model.find_by_sql("select * from models where id = 0") > > gave me empty array if entry was not there > else array with instance of model matching criteria. > > hope this helps > > regards > gaurav > > > > On 1/3/07, Ryan <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > > > > gaurav bagga wrote: > > > hi, > > > > > > try > > > > > > x=Model.find(some_criteria).field || Model.new > > > > > > in case you dont require Model.new can use "" as message for nothing > > > > > > ~gaurav > > > > That doesn''t work for me. I get a ActiveRecord::RecordNotFound error. > > > > -- > > Posted via http://www.ruby-forum.com/. > > > > > > >You can also do this... x=Model.find(criteria).field rescue "default value" _Kevin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
anibalrojas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jan-04 13:19 UTC
Re: ActiveRecord find exceptions
Sebastian Delmont has an interesting solution for this situations, he
posted it at his blog:
http://www.notsostupid.com/blog/2006/06/28/do-or-do-not-or-maybe-try/
Currently offline, but you can checkit at googles cache:
http://64.233.161.104/search?q=cache:4KIQLnwdpLYJ:www.notsostupid.com/blog/2006/06/28/do-or-do-not-or-maybe-try/+site:www.notsostupid.com+exception&hl=en&ct=clnk&cd=1&client=firefox
Basically what you do is:
module Kernel
# Returns ''value'' in case of an exception, otherwise returns
the
execution
# of the given block
def try(value = nil)
yield if block_given?
rescue Exception => exception
value
end
end
Hope this simplies your exception handling for this situations
--
AnĂbal Rojas
http://www.rubycorner.com
http://www.hasmanydevelopers.com/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---