Abram
Abram

Reputation: 41954

How to query the associations of an association

I want to do the following to get a list of all the reviews for a business:

@business.professionals.reviews

I am not concerned about which professional the reviews belong to, but I want the reviews returned in the same format as professionals would be returned in @business.professionals

In my example, businesses have many professionals, and professionals have many reviews.

Upvotes: 0

Views: 39

Answers (1)

usha
usha

Reputation: 29369

Add this to your business model

class Business < ActiveRecord::Base
  has_many :professionals
  has_many :visible_reviews, :through => :professionals, :class_name => "Review", :source => :reviews :conditions => ['prefessionals.reviews_visible = ?',true]
  has_many :reviews, :through => :professionals
end

And now you can get all the reviews of all professionals using

@business.visible_reviews

Upvotes: 2

Related Questions