I have the following models:
class Audit < ActiveRecord::Base
has_one :country, :through => :another_model
has_and_belongs_to_many :questions
end
class Question < ActiveRecord::Base
has_and_belongs_to_many :audits
has_many :details # one detail for each country [england, wales, scotland etc]
has_one :detail, :conditions?
# i want it to have one detail dynamically depending on the audit
end
class Detail < ActiveRecord::Base
belongs_to :country
belongs_to :question
end
The problem is regarding the question
model. Say for example I have Audit A with Questions 1, 2 and 3 and Audit A is based in the country England. When viewing Audit A I want to display the 3 Questions; no problem. @audit.questions.each do |q|
etc
Now let's explain the question details. Let's say I have Detail X, Y and Z. Detail X, Y and Z are all associated with Question 1. However, Detail X is based in the country England, Detail Y in Wales and Detail Z in Scotland. At the moment I have all details pulled out of the database and then in the view/model I get the current audit's country and subsequently get the detail based off that.
Is there a way to say something along the lines of:
class Question < ActiveRecord::Base
has_and_belongs_to_many :audits
has_one :detail, :conditions -> {country_id = current_audit_being_viewed.country.id}
end
I'm also aware I could just be thinking about the relations in the wrong way so please feel free to just tell me a better way all together to do what I'm trying to achieve.