ruby on rails - Calculate offset of a row -
i writing server side on ror, uses postgresql.
today faced feature, in need identify current offset of row in table. client send me unique identifier , need return current offset of item.
so, have table 'items' , model 'item'.
item has unique field. let's call 'key'.
for instance, 'items' table has data:
id | key ---------- 0 | abb 1 | acc 3 | cbb 5 | aqq if client send me key 'aqq'. should output 4. if client send me 'acc'. should output 2.
if there way without loading data table , going through in loop?
thanks in advance!
if wanted through activerecord like:
item.where("id <= ?", item.find_by_key("aqq").id).count
Comments
Post a Comment