Three models:
class Customer < ActiveRecord::Base
has_many :visits
end
class Visit < ActiveRecord::Base
belongs_to :customer
has_many :messages
end
class Message < ActiveRecord::Base
belong_to :visit
end
Now I want to return all of the customers visits in which they have messages. So in pseudo code something like this:
@customer = Customer.find(:id)
@customer.visits.where(visit has messages)
How do I do something like this?
Do an inner join: (recommended):
Make sure to deal with duplicates
With possible performance issues, another option is to use SQL EXISTS clause:
Or SQL IN:
http://guides.rubyonrails.org/active_record_querying.html#using-array-hash-of-named-associations