Hi everyone,
I have a three-table system: Companies, Customers, and Matches. The matches include, for example, private notes that each party makes about the other and some options they can set regarding the other.
I have two sets of views centered around Companies and Customers for each party to look at the other and modify their notes and settings.
# Customers Controller
def show
@customer = Customer.find(params[:customer])
@matchings = @customer.matchings.find... #corrected
@company = Company.find(params[:company])
end
Obviously the @matchings is incomplete. Given that @matchings has fields customer_id and company_id, how do I find the right matching record? Thank you!
------UPDATE------
That was really helpful, thank you jdl!
The company asks the customer some questions and the customer responds. Questions are stored in Question and answers are stored in MatchingAnswer. To clarify the database's structure:
# models, aggregated
1st set of connections:
Company has_many :questionlists (QuestionList belongs_to :company)
QuestionList has_many :questions (Question belongs_to :question_list)
Question has_many :matching_answers (MatchingAnswer belongs_to :question)
2nd set of connections:
Company has_many :matchings (Matching belongs_to :company)
Matching has_many :matching_answers (MatchingAnswer belongs_to :matching)
Remember matching is the 'through' table between Company and Customer. I have to show to the company each question the company has asked and each answer to that question from a particular customer.
# QuestionLists controller
@questions = @questionlist.questions.find(:all)
@matchinganswers = @matching.matching_answers.find(:all, :conditions => ["question_id= ?", @question.id])
...except the condition needs to be satisfied by each question id supplied by my loop INSIDE the view. Let's take a look at the view.
# view
<% @questions.each do |q| %>
<li><%= q.question %></li>
<li><% q.matching_answers.each do |a| %>
<%= a.answer %>
<% end %></li>
<% end %>
I know this is riddled with holes.. I just haven't been able to fill them properly. How can I do the loop so underneath each question we see the answers the customer gave to that question? Thank you so much guys, this is an amazing group. :)
------UPDATE #2------
The problem is not the nesting, it is that q.matching_answers gives me EVERY answer that every customer has ever given to question q. I need to generate only those matching_answers that both stem from a particular question and match our matching_id (@matching.id). The MatchingAnswer model contains a matching_id field so each answer is unique to that specific match between customer and company. My problem is setting the right parameters so I get only those matching_answers that satisfy:
MatchingAnswer.matching_id = @matching.id
MatchingAnswer.question_id = @question.id
The problem is, while I DO have @matching.id calculated in the QuestionLists controller, I only have access to @question_list (there's only one) and @questions (all the questions belonging to that list). I thought I have to do a loop inside the view that says, for each question, give me those questions that satisfy MatchingAnswer.question_id = [this question.id]. I've been trying to do it with the above nest, but as you can tell, it doesn't give us the two parameters we need.