ruby on rails 3 - Limit ignored when used with find_by_id -
i trying understand behaviour of rails activerecord limit
.
let's have model car
1 attribute name
.
now in console (rails console
), if type:
car.limit(0).where(name: 'foo')
i get
select "cars".* "cars" "cars"."name" = 'foo' limit 0
which correct, but:
car.limit(0).find_by_id(1)
gives
select "cars".* "cars" "cars"."id" = 1 limit 1
why limit
option ignored?
note that
car.where(name:'foo').find_by_id(1)
gives
select "cars".* "cars" "cars"."name" = 'foo' , "cars"."id" = 1 limit 1
so find_by_id
(or find
) ignores limit
accepts where
? how explain that?
sidenote
i know these queries don't make sense. have edge case in project executes car.limit(0).find_by_id(1)
, expected result nil object. trying understand why find_by_id
or find
ignores limit
.
find
, find_by_
methods used retrieve single instance. result either 1 object or nil if there no objects found.
because method supposed return 1 object sets limit 1 regardless of limit specify.
if want specify limit, use per example.
Comments
Post a Comment