ruby on rails - find vs find_by vs where -
i new rails. see there lot of ways find record:
find_by_<columnname>(<columnvalue>)
find(:first, :conditions => { <columnname> => <columnvalue> }
where(<columnname> => <columnvalue>).first
and looks of them end generating same sql. also, believe same true finding multiple records:
find_all_by_<columnname>(<columnvalue>)
find(:all, :conditions => { <columnname> => <columnvalue> }
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
Post a Comment