1

I am confused on where to use '.find' and where to use 'where'. is there a difference in the performance during the execution of the query ??

example : converted the existing queries which are using .find as below :

FileOrFolder.find_by_fullpath(completePath, :select=>"id") -->

FileOrFolder.where(fullpath: completePath).select(:id).first

Component.find(:first, :conditions=>["cluster_id = ? AND name = ?", cluster_id, key]) -->

Component.where(cluster_id: cluster_id, name: key).first
Vinay
  • 237
  • 2
  • 8
  • 17
  • No difference in performance - same queries, but more "handy" to use `where` because of the returned ActiveRecord::Relation ( http://stackoverflow.com/questions/9574659/rails-where-vs-find ) – MrYoshiji Jun 26 '13 at 16:03
  • @MrYoshiji ... i have gone through this post .... no where it talks about the query performance right .....pls correct me if am wrong – Vinay Jun 26 '13 at 16:06
  • You could just do it yourself by testing in your console `Component.where(id: 1).first` and `Component.find(1)`, you would see that is produces the EXACT same queries and take the EXACT same time because they are ... the same queries! – MrYoshiji Jun 26 '13 at 16:15

3 Answers3

8

These are all equivalent. What you're seeing here is the evolution of the ActiveRecord query syntax from before AREL was incorporated. The older style dynamic finders are still valid, though.

This syntax is from ROR 2.x and earlier using dynamic finders:

FileOrFolder.find_by_fullpath(completePath, :select=>"id")

Whereas these are more in the ROR 3.x style:

FileOrFolder.where(fullpath: completePath).select(:id).first
Component.where(cluster_id: cluster_id, name: key).first

And your last example using find is valid in either context.

Component.find(:first, :conditions=>["cluster_id = ? AND name = ?", cluster_id, key])

When in doubt, consult the ROR query guide.

I personally find the where styles are very useful when you're building up a query over several lines of code and not all at once. Since they defer execution until the latest moment, they let you build the query piecemeal.

Dave S.
  • 6,349
  • 31
  • 33
1

It doesn't make a significant performance difference because usually they will both generate the same SQL, e.g.:

Article.find_by_headline('foo')
=> SELECT `articles`.* FROM `articles` WHERE `articles`.`headline` = 'foo' LIMIT 1

Article.where(headline: 'foo').first
=> SELECT `articles`.* FROM `articles` WHERE `articles`.`headline` = 'foo' LIMIT 1
Mori
  • 27,279
  • 10
  • 68
  • 73
  • so you mean to say ...its just a syntax difference overall ..... as some find_by_headline type of syntax is being deprecated, we are tending to use where .... thts it right ? – Vinay Jun 26 '13 at 16:12
  • @Vinay, `find_by` is _not_ being deprecated, just those other variations. My preference is to use whichever syntax is more clear & concise for the current case, with the tie going to `where`. In practice I use both. I think that's more personal aesthetics than a uniform best practice though. – Mori Jun 26 '13 at 16:47
0

You should prefer where style going forward, Rails 4 has deprecated use of find_by_ - see http://edgeguides.rubyonrails.org/4_0_release_notes.html#active-record-deprecations

house9
  • 20,359
  • 8
  • 55
  • 61
  • 1
    From your link: All dynamic methods *except* for find_by_... and find_by_...! are deprecated. Here's how you can rewrite the code: – notapatch Feb 02 '15 at 22:27