You can use the has_many :through association. In your models:
# Facility model has_many :tags has_many :inspections, :through => :tags
And you can get all the checks as follows:
@inspections = Facility.find(params[:facility_id]).inspections
But if you have a HABTM relationship between Facility and Tag, it will be more complicated and you will have to write several sql files manually, for example:
@inspections = Inspection.all(:joins => "INNER JOIN tags ON tags.id = inspections.tag_id INNER JOIN facilities_tags ON tags.id = facilities_tags.tag_id", :conditions => ["facilities_tags.facility_id = ?", params[:facility_id] )
Of course, your table structure depends on the code. If you show this, then it would be easier to give the right answer :). Hope this helps!
source share