Hi,
I have a polymorphic association between Company and Address.
class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic => true
end
class Company < ActiveRecord::Base
has_many :addresses, :as => :addressable, :dependent => :destroy
end
I want to fetch all addresses that fulfills conditions on both Company
and Address. The following query does not work but it illustrates what
I want to achieve.
Address.all :joins => :companies, :conditions => { :companies =>
{ :reseller => true }, :addresses => { :geocoded => true } }
The following SQL query does the job but I want to do it with a single
Active Record Query. Is that possible?
SELECT addresses.* FROM addresses
INNER JOIN companies ON companies.id = addresses.addressable_id AND
addresses.addressable_type = 'Company'
WHERE (addresses.geocoded = 't'
AND addresses.address_type = 2
AND companies.reseller = 't'
AND companies.enabled = 't')
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---