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










-- 


Nick


-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to