开发者

How do I fetch just the ids via ActiveRecord

class PersonAddress < ActiveRecord::Base
end

I would like to fetch only the PersonAddress ids (primary keys) using a ActiveRecord query, how should I do that, something similar to PersonAddres开发者_运维问答s.find_all_by_person_id(person.id) which returns a set of address ids alone. (e.g. if the person has 3 addresses, then it should return 3 ids and not PersonAddress objects)


What you could do is using :select parameter:

PersonAddress.find_all_by_person_id(person.id, :select => :id).map(&:id)
=> [2, 3, 5]  # Fake ids


Depends on which Rails version you are using... but this might work:

PersonAddress.find_all_by_person_id(person.id).select("id")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜