HABTM checking for match of latest 3
Here's an interesting one for you folks...
I have a HABTM (has_and_belongs_to_many) relationship between "Dogs" and "Trips". My goal is to find two result sets: 1) Dogs that have been on at least 1 of the last 3 trips and call that @dogs_current 2) Dogs that have NOT been on any of the last 3 trips and call that @dogs_old
I found that I can find what the last 3 trips are by doing this in the Trip model:
named_scope :last3, :order => 'date DESC', :limit => 3
But not sure how to use that list get 1 and 2. This hack works, but it seems ugly. There must be a better way! :)
@dogs_current = []
@dogs_old = []
@dogs.each do |dog|
if (Trip.last3 - dog.trips).size < 3 then
@dogs_current << dog
else
开发者_开发技巧@dogs_old << dog
end
end
Any ideas? Thanks! -Cam
class Trip < ActiveRecord::Base
has_and_belongs_to_many :dogs
named_scope :last3, :order => 'date DESC', :limit => 3
end
class Dog < ActiveRecord::Base
has_and_belongs_to_many :trips
end
#IDs of last 3 trips
last_trips_ids = Trip.last3.collect(&:id)
# I'm assuming your join table is `dogs_trips`.
@dogs_current = Dog.all(:joins => :trips,
:conditions => ["dogs_trips.trip_id IN (?)", last_trips_ids]).uniq
@dogs_old = Dog.all(:joins => :trips,
:conditions => ["dogs_trips.trip_id NOT IN (?)", last_trips_ids]).uniq
I believe this is correct. At least worked here for me...
精彩评论