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

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -