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 rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
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 -Prem On Wednesday, March 6, 2013 at 10:38 PM, Nick Ostrovsky wrote:> > > > > 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 rubyonrails-core+unsubscribe@googlegroups.com (mailto:rubyonrails-core+unsubscribe@googlegroups.com). > To post to this group, send email to rubyonrails-core@googlegroups.com (mailto:rubyonrails-core@googlegroups.com). > Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > >-- 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 rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
On Mar-07-2013, at 9:08 AM, Nick Ostrovsky <nick@firedev.com> wrote:> User.where(hash: hash) # .firstWouldn''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. -- Tejas Dinkar http://c42.in C42 Engineering - The guys behind RubyMonk -- 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 rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
or User.find_by! hash: hash 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) https://github.com/frodsan http://www.frodsan.com/ On Wednesday, March 6, 2013 at 11:06 PM, Tejas Dinkar wrote:> On Mar-07-2013, at 9:08 AM, Nick Ostrovsky <nick@firedev.com (mailto:nick@firedev.com)> wrote: > > User.where(hash: hash) # .first > > > > > 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. > -- > Tejas Dinkar > http://c42.in > C42 Engineering - The guys behind RubyMonk > > -- > 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 rubyonrails-core+unsubscribe@googlegroups.com (mailto:rubyonrails-core+unsubscribe@googlegroups.com). > To post to this group, send email to rubyonrails-core@googlegroups.com (mailto:rubyonrails-core@googlegroups.com). > Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > >-- 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 rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
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. -- Nick On Thursday, 7 March 2013 at 10:38, Nick Ostrovsky wrote:> > > > > 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 rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.