Bangin Finders plugin
Plugin details
Documentation
ruby script/plugin install http://sethrasmussen.com/svn/rails/plugins/bangin_finders
Those who know ActiveRecord are familiar with the dynamic attribute finders. Given a model with attrbutes :foo, :bar and :baz, one gets the following finders, handled via :method_missing:
* find_by_foo, to search for a single record * find_all_by_foo, to search for all records * repeat for each attribute...
Those finders act like a search, happily returning nil or an empty Array. Sometimes you may wish to use another attribute than the primary key as the primary identifier in your routes. In that case, you'd have to write your own finder to raise ActiveRecord::RecordNotFound if you want to use the built-in 404-rendering behavior.
Now, you can simply use those familiar dynamic finder names.. with a bang!
>> Page.find_by_url! 'about' => # >> Page.find_by_url! 'aboutyourmom' ActiveRecord::RecordNotFound: no record(s) found for "find_by_url" with ["aboutyourmom"]
This could be useful for a simple, friendly URL system where, when given a params[:id] of 'Bort' and having no users by that name, you'd like to raise as if you were doing a :find on the primary key.
Further Documentation
There is currently no advanced documentation for this plugin.
New documentationEdit plugin | (0 older versions) | Last edited by: Guest, 2 months ago

