开发者

How do I fetch just the ids via ActiveRecord

开发者 https://www.devze.com 2023-04-11 23:53 出处:网络
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开发
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

精彩评论

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