I’ve been trying to figure this out for a while, but what is the best way to find all principals based on their single dependents properties. For example, let’s say I have these models:
class Principal < ActiveRecord::Base
has_one :dependent
end
class Dependent < ActiveRecord::Base
belongs_to :principal
attr_accessible :color
end
How can I query the database to give me back all the Principals whose dependents have the color blue? If the answer isn’t the same, what if I wanted a range (say :color was actually :number, and I wanted all the principals whose dependent’s number fall within the range of 10-20).
Here is the closest solution I have to this problem, I’m sure it’s terribad:
principal_collection = Array.new
Dependent.where(:color => 'blue').each do |d|
principal_collection << d.principal
end
Thanks in advance 🙂 I apologize if the answer is out there, I tried searching.
Maybe you are finding this?