ActiveRecord find exceptions

hi,

try

x=Model.find(some_criteria).field || Model.new

in case you dont require Model.new can use “” as message for nothing

~gaurav

hi,

in my previous reply I dint think of the exception hence :frowning: 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

gaurav bagga wrote:

hi,

  in my previous reply I dint think of the exception hence :frowning: 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

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