2008年9月18日 星期四

include, map


user 拿全部的x_alerts

[model]
belongs_to :user
has_many :x_alerts, :extend => XAlert::AssociationMethods, :through => :x_alert_relationships
has_many :x_alert_relationships



a = u.alert_targets.find(:all, :include => {:x_alert_relationships => :x_alert} )

AlertTarget Load (0.000180) SELECT * FROM `alert_targets` WHERE (`alert_targets`.user_id = 2590)
XAlertRelationship Load (0.000152) SELECT `x_alert_relationships`.* FROM `x_alert_relationships` WHERE (`x_alert_relationships`.alert_target_id IN (1,2,10,11))
XAlert Load (0.000212) SELECT * FROM `x_alerts` WHERE (`x_alerts`.id IN ('6','216','7','8','250','229','218','241','197','214','104','5'))

a.map {|e| xar = e.x_alert_relationships; xar.map { |k| h = k.x_alert; h[:cat] = k.created_at; h}}.flatten

沒有留言: