1

ホスト レコード (id、hostname、netid) を持つテーブルと、2 つのルート間の接続を表すルーティング接続を持つテーブル (id、src_id、src_ip、dst_id、dst_ip) をモデル化しようとしています。

ホストのすべてのルーティング レコードを取得し、(ホスト名を取得できるように) ホスト テーブルを :include しようとすると、ルーティング レコードのみが取得されます。私は何を間違っていますか?

class Host < ActiveRecord::Base
  has_many :routes
end

class Route < ActiveRecord::Base
  belongs_to :srchost, :class_name => 'Host', :foreign_key => 'src_id'
  belongs_to :dsthost, :class_name => 'Host', :foreign_key => 'dst_id'
end

x = Route.where(:src_id => host).includes(:srchost, :dsthost)
puts x.inspect
4

1 に答える 1

1

インクルードは関連付けモデルを返さないため、カスタム sql を使用します。Route.find_by_sql('select hosts.* ...') または、includes と select('hosts.*') の代わりに結合を使用できます。例:

User.joins(:posts).select('posts.title')
于 2013-05-15T19:52:14.400 に答える