ruby on rails - find vs find_by vs where -


i new rails. see there lot of ways find record:

  1. find_by_<columnname>(<columnvalue>)
  2. find(:first, :conditions => { <columnname> => <columnvalue> }
  3. where(<columnname> => <columnvalue>).first

and looks of them end generating same sql. also, believe same true finding multiple records:

  1. find_all_by_<columnname>(<columnvalue>)
  2. find(:all, :conditions => { <columnname> => <columnvalue> }
  3. where(<columnname> => <columnvalue>)

is there rule of thumb or recommendation on 1 use?

use whichever 1 feel suits needs best.

the find method used retrieve row id:

model.find(1) 

other uses of find replaced things this:

model.all model.first 

find_by used helper when you're searching information within column, , maps such naming conventions. instance, if have column named name in database, you'd use following syntax:

model.find_by_name("bob") 

however, believe find_by being deprecated.

.where more of catch lets use bit more complex logic when conventional helpers won't do.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -