문제

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? Rails 4 Release Notes conveniently avoid this.

Update: It plainly says: "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.

도움이 되었습니까?

해결책 3

It plainly says: "All dynamic methods EXCEPT for find_by_... and find_by_...! are deprecated."

다른 팁

I think the new syntax is

User.find_by!(hash: hash)

At least that's how ryanb does it: http://railscasts.com/episodes/400-what-s-new-in-rails-4

Hope that helps.

Well, 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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top