How to return a subset of multiple collections and its related model in Ruby on Rails?
Working with Rails 3 here.
I have a model named 'employee' that has many addresses and has many contacts as such:
class Employee < ActiveRecord::Base
has_many :addresses
has_many :contacts
What I want to do is to be able to write a single query that returns a specific Employee along with all the addresses created after a specified date and with all contacts created after that same date. If this is possible, how would I craft such a statement?
I have scopes defined on the address and contact models as such:
scope :created_after, lambda { |date_time| where('created_at >= ? or updated_at >= ?', date_time, date_time}
... but I can't come up with a way to craft a SINGLE query returning the Emplo开发者_运维百科yee and a filtered subset of each collection in one swoop. Any ideas on how to do so efficiently would be appreciated.
Thanks -wg
Perhaps something like:
Employee.find(specific_user_id,
:joins => :addresses,
:conditions => {
:addresses => ["created_at >= ? OR updated_at >= ?", time, time]
}
)
精彩评论