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")
精彩评论