我能否获得帮助,将使用 JOINS 的 SQL 查询转换为 ruby 语句?

Can I get help converting an SQL query using JOINS to a ruby statement?

我正在使用 ruby v2 rails v3.2 和 postgreSQL v8.4。

我目前有这个 activerecord 查询:

@ct_a = DateTime.strptime('03-18-2015 22:08', '%m-%d-%Y %H:%M')
@ct_b = DateTime.strptime('03-20-2015 22:08', '%m-%d-%Y %H:%M')

events = Event.ips_events.all(conditions: ['timestamp >= ? AND timestamp <= ?', @ct_a, @ct_b ]).reverse

在 SQL 中,此查询大致如下所示:

SELECT *
  FROM "event"
    WHERE (timestamp >= '2015-03-18 22:08:00' AND timestamp <= '2015-03-20 22:08:00') 
      ORDER BY event.timestamp desc;

我想使用连接将上述 events 语句更改为更高效的语句。

我构建了 SQL 查询,它在一个查询中从四个 table 中给出了我想要的输出。

SELECT "event".cid,
       "event".sid,
       "signature".sig_id,
       "signature".sig_name,
       "sig_class".sig_class_name,
       "signature".sig_priority,
       "signature".sig_rev,
       "signature".sig_sid,
       "signature".sig_gid,
       "iphdr".ip_src,
       "iphdr".ip_dst,
       "iphdr".ip_proto,
       "event".timestamp
  FROM "event" 
    JOIN signature ON (event.signature = signature.sig_id)
      JOIN sig_class ON (signature.sig_class_id = sig_class.sig_class_id)
        JOIN iphdr ON iphdr.cid = event.cid AND iphdr.sid = event.sid
          WHERE (timestamp >= '2015-03-18 22:08:00' AND timestamp <= '2015-03-20 22:08:00') 
            ORDER BY event.timestamp desc;

如何在 ruby 中对 rails activerecord 查询执行此操作?我试过 find_by_sql 但我只取回事件 table,而不是像原始 SQL.

那样加入

我的模特:

事件:与签名的关系是 Event.signature => Signature.sig_id

class Event < ActiveRecord::Base
  # DB Schema
  # sid | cid | signature | timestamp
  #----------------------------------

  attr_accessible :sid, :cid, :signature, :timestamp
  self.primary_keys = :sid, :cid
  self.table_name = 'event'
  has_many :sensors,
           :foreign_key => :sid
  has_many :signatures,
           :foreign_key => :sig_id,
           :primary_key => :signature
  has_many :ip_hdrs,
           :foreign_key => [:sid, :cid]
  has_many :tcp_hdrs,
           :foreign_key => [:sid, :cid]
  has_many :udp_hdrs,
           :foreign_key => [:sid, :cid]

  def self.ips_events
    Event.order('event.timestamp desc').includes(:signatures,:ip_hdrs,:tcp_hdrs,:udp_hdrs)
  end
end

签名:与 SigClass 的关系是 Signature.sig_class_id => SigClass.sig_class_id

class Signature < ActiveRecord::Base
  # DB Schema
  # sig_id | sig_name | sig_class_id | sig_priority | sig_rev | sig_sid | sig_gid

  attr_accessible :sig_id, :sig_name, :sig_class_id, :sig_priority, :sig_rev, :sig_sid, :sig_gid
  self.table_name = 'signature'
  self.primary_key = :sig_id

  belongs_to :event,
             :foreign_key => :sig_id

  belongs_to :sig_class

  has_many :references,
           :through => :sig_references

  has_many :sig_references,
           :foreign_key => :sig_id
end

SigClass:

class SigClass < ActiveRecord::Base
  # DB Schema
  # sig_class_id | sig_class_name
  #-------------------------------
  attr_accessible :sig_class_id, :sig_class_name
  self.primary_key = :sig_class_id
  self.table_name = 'sig_class'
  has_many :signatures
end

IpHdr:与事件的关系是 Event.cid 和 Event.sid => IpHdr.cid 和 IpHdr.sid

class IpHdr < ActiveRecord::Base
  # DB Schema
  # sid | cid | ip_src | ip_dst | ip_ver | ip_tos | ip_len | ip_id | ip_flags | ip_off | ip_ttl | ip_proto | ip_csum
  #-----------------------------------------------------------------------------------------------------------------

  attr_accessible :sid, :cid, :ip_src, :ip_dst, :ip_ver, :ip_tos, :ip_len, :ip_id, :ip_flags, :ip_off,
                  :ip_ttl, :ip_proto, :ip_csum
  self.primary_keys = :sid, :cid
  self.table_name = 'iphdr'
  belongs_to :event,
             :foreign_key => [:sid, :cid]
end
Event.ips_events.joins([:ip_hdrs, {signatures: sig_class}])