我试图根据非标准外键对连接进行选择。 SQL看起来不错,但Rails并没有吐出结果。
我有一个像这样的消息模型:
class Message < ActiveRecord::Base
belongs_to :sender, :class_name => 'Person',
:foreign_key => 'sender_id'
belongs_to :recipient, :class_name => 'Person',
:foreign_key => 'recipient_id'
end
和这样的人物模型:
class Person < ActiveRecord::Base
with_options :class_name => "Message", :dependent => :destroy,
:order => 'created_at DESC' do |person|
person.has_many :_sent_messages, :foreign_key => "sender_id",
:conditions => "sender_deleted_at IS NULL"
person.has_many :_received_messages, :foreign_key => "recipient_id",
:conditions => "recipient_deleted_at IS NULL"
end
end
如果我像这样查询表格:
Message.joins(:sender).select('messages.id, people.latitude')
我得到了合理的SQL形式:
"SELECT messages.id, people.latitude FROM \"messages\" INNER JOIN \"people\" ON \"people\".\"id\" = \"messages\".\"sender_id\""
此SQL在控制台中执行正常并返回结果。
但是我的Rails结果集看起来像这样
[#<Message id: 933>, #<Message id: 943>, #<Message id: 946>, ...]
未返回纬度或经度值。如果我使用person_id作为外键,相同的查询工作正常。我使用的是Postgres。
为什么会这样,我怎样才能获得纬度和经度值?
谢谢!