In what situations should be used Model.where or Model.find() ? Everywhere is information on the differences, but what is a recommendation for their use?
In what situations should be used Model.where or Model.find() ? Everywhere is information on
Share
wheredoesn’t actually fetch anything – it returns anActiveRecord::Relationscoped with the conditions you’ve just specified. This means you can chain it with stuff: more calls towhere,joins,limitetc.findon the other can executes the query and returns an array or an object (depending on whether you dofind :allorfind :first, so you can’t chain stuff on the back of it.You can pass conditions, joins etc. to find but doing so will become deprecated. You don’t really ever need to call
findsince you either let theActiveRecord::Relationturn itself into an array when it needs to or callall/first/laston the relation, which achieves the same thing that callingfindwould do.About the only thing
findis handier for is when you have an id (or array of ids) and want to load the corresponding model objects.