13

When I call a find with an id, it becomes a targeted find, and will throw an error RecordNotFound.

Foo::Bar.find(123) # RecordNotFound if no Bar with id 123 exists.

But when I call that with conditions, I get nil if not found:

Foo::Bar.find(:first, :conditions => [ "lower(name) = ?", name.downcase ])

I want such a conditional search to raise an error too. I know I can do:

Foo::Bar.find_by_name!("CocktailBar") #=> raises Recordnotfount if not not found.

But that has only really simple conditions. Mine need a little more complexity; actually something like:

Foo.Bar.select{ |pm| pm.name.downcase =~ /cocktail/}.first

And, if nothing is found, I want it to raise the RecordNotFound error. Is that possible at all? Or should I simply add some code to check against nil? and if nil? raise the error myself? And if so, how do I do that in Rails 3?

3 Answers 3

47

In the last snippet of code you are actually fetching all records from DB and then doing select on a Ruby array. It has nothing to do with ActiveRecord, so you can do whatever you like to raise exception manually, either use the code suggested by Douglas, or if, or unless etc. But it seems that you don't quite understand what your code does. Your select {...} is not translated into SQL SELECT ... WHERE(...).

If you need an exception raised automatically by ActiveRecord query, use this:

Foo::Bar.where([ "lower(name) = ?", name.downcase ]).first!

The equivalent bang methods exist for find_by methods as well, for example Foo::Bar.find_by_name!(name)

3
  • 4
    Never knew about the first! method, thanks for mentioning it.
    – bricker
    Commented Sep 13, 2012 at 5:40
  • @lulalala Thanks for extending my answer. I didn't care to mention the find_by methods because the question author explicitly says he knows them and the question is not quite about this.
    – RocketR
    Commented Jun 13, 2013 at 9:16
  • @RocketR I just realized it in mentioned in question now. I guess I was focusing on the question title.
    – lulalala
    Commented Jun 13, 2013 at 9:20
8

For anyone coming across this question, now you have the method find_by! that does exactly what the OP asked for the find case.

example:

Foo::Bar.find_by!(name: "CocktailBar")

https://apidock.com/rails/v4.0.2/ActiveRecord/FinderMethods/find_by%21

3

Your final paragraph is what you need to do. Either check against nil, or raise the exceptions yourself. To raise the exception yourself, do the following:

Foo::Bar.find(:first, :conditions => [ "lower(name) = ?", name.downcase ]) || raise(ActiveRecord::RecordNotFound)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.