# Dynamic bang finders in Rails 4

**URL:** <https://discuss.rubyonrails.org/t/dynamic-bang-finders-in-rails-4/66516>\
**Category:** rubyonrails-core\
**Created:** [March 7, 2013, 3:38am UTC](https://discuss.rubyonrails.org/t/dynamic-bang-finders-in-rails-4/66516 "2013-03-07T03:38:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick\_Ostrovsky](https://avatars.discourse-cdn.com/v4/letter/n/ecc23a/32.png) [@Nick\_Ostrovsky](https://discuss.rubyonrails.org/u/Nick_Ostrovsky)\
**Post date:** [March 7, 2013, 3:38am UTC](https://discuss.rubyonrails.org/t/dynamic-bang-finders-in-rails-4/66516/1 "2013-03-07T03:38:17Z")

</div>

Hi,

I propose to add #where! finder that raises an exception if the relation is empty like find\_by\_xxx! method in Rails3.

Rails 4 is getting rid of dynamic finders, so

```
User.find_by_hash(hash)

```

becomes

```
User.where(hash: hash) # .first

```

Okay, not a big deal. But what is the best way to deal do with dynamic bang finders like User.find\_by\_hash!(hash) since there is no where!() method?

A suggestion I got on Stackoverflow ([Dynamic bang finders in Rails 4 - Stack Overflow](http://stackoverflow.com/questions/14372963/dynamic-bang-finders-in-rails-4/15257207)) is to add a #where! method.

If you need a method that finds all but raises exception if the relation is empty, you can create such new method for your models yourself (or mixin to ActiveRecord::QueryMethods). Something like:

```
def where!(*args)

	  rel = where(*args) 

	  raise RecordNotFound if rel.empty?

	  rel

end

```

---

<div class="post-metadata">

**Author:** ![sikachu](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/sikachu/32/628_2.png) [@sikachu](https://discuss.rubyonrails.org/u/sikachu)\
**Post date:** [March 7, 2013, 3:41am UTC](https://discuss.rubyonrails.org/t/dynamic-bang-finders-in-rails-4/66516/2 "2013-03-07T03:41:25Z")

</div>

No, those ‘dynamic finders’ based on your attribute names aren’t get removed. If you have an attribute name `hash`, you should be able to still do `User.find_by_hash!`

See the list of deprecated methods here: [http://edgeguides.rubyonrails.org/4\_0\_release\_notes.html#active-record-deprecations](http://edgeguides.rubyonrails.org/4_0_release_notes.html#active-record-deprecations)

-Prem

---

<div class="post-metadata">

**Author:** ![Tejas\_Dinkar](https://avatars.discourse-cdn.com/v4/letter/t/b5ac83/32.png) [@Tejas\_Dinkar](https://discuss.rubyonrails.org/u/Tejas_Dinkar)\
**Post date:** [March 7, 2013, 4:06am UTC](https://discuss.rubyonrails.org/t/dynamic-bang-finders-in-rails-4/66516/3 "2013-03-07T04:06:09Z")

</div>

Wouldn’t it be more intuitive to use the #first! method for this purpose?

i.e., User.find\_by\_hash!(hash) is the same as User.where(hash: hash).first!

I know that I often chain where conditions together (especially when using scopes), so it makes sense to raise the exception only when I actually try to fetch the record from the database.

---

<div class="post-metadata">

**Author:** ![Francesco\_Rodriguez](https://avatars.discourse-cdn.com/v4/letter/f/bb73d2/32.png) [@Francesco\_Rodriguez](https://discuss.rubyonrails.org/u/Francesco_Rodriguez)\
**Post date:** [March 7, 2013, 4:51am UTC](https://discuss.rubyonrails.org/t/dynamic-bang-finders-in-rails-4/66516/4 "2013-03-07T04:51:54Z")

</div>

or User.find\_by! hash: hash

[http://edgeapi.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find\_by-21](http://edgeapi.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find_by-21)

Francesco Rodríguez "The Most Powerfull Rookie"

@frodsan ([http://twitter.com/#!/frodsan](http://twitter.com/#!/frodsan)) [frodsan (Francesco) · GitHub](https://github.com/frodsan)[http://www.frodsan.com/](http://www.frodsan.com/)

---

<div class="post-metadata">

**Author:** ![Nick\_Ostrovsky](https://avatars.discourse-cdn.com/v4/letter/n/ecc23a/32.png) [@Nick\_Ostrovsky](https://discuss.rubyonrails.org/u/Nick_Ostrovsky)\
**Post date:** [March 7, 2013, 4:54am UTC](https://discuss.rubyonrails.org/t/dynamic-bang-finders-in-rails-4/66516/5 "2013-03-07T04:54:36Z")

</div>

Oh, right “All dynamic methods EXCEPT for `find_by_...` and `find_by_...!` are deprecated.”

Either the pages has changed since or I was blind when reading it.
