Reputation: 1302
I have 3 tables: Phonecalls, Results, Questions.I have 3 models also : Phonecall, Result, Question
Schema of Phonecalls is: id, date,user_id
Schema of Questions is: id, text,error
Schema of Results is: id, phonecall_id, question_id, result
My Phonecall.rb is:
class Phonecall < ActiveRecord::Base
has_many :results
has_many :questions, :through => :results
end
My Result.rb is:
class Result < ActiveRecord::Base
has_one :question
belongs_to :phonecall
end
And my Question.rb:
class Question < ActiveRecord::Base
belongs_to :result
has_many :phonecalls, :through => :result
end
So how i can get Question properties in this loop:
<%
@phonecalls.each do |ph|
ph.results.each do |r|
%>
<%= ph.id %>
<%= r.id %>
<%
end
end
%>
If i try to get r.question.id - i have error. If i try to do some loop with ph.questions - i have error.
How i can do it?
Upvotes: 0
Views: 615
Reputation: 1302
Yes, my problem was in my relationships.
in Result.rb i told that result belogs to question but it's false. Now i have this relatioships: Result.rb:
class Result < ActiveRecord::Base
belongs_to :question
belongs_to :phonecall
end
Questio.rb
class Question < ActiveRecord::Base
has_many :result
has_many :phonecalls, :through => :result
end
It's work for me.
Upvotes: 0
Reputation: 15987
has_many :phonecalls, through: :result
allows you to do:
question.phonecalls
that will give you back an array of phonecalls
associated with your one question
instance.
Inversely and to answer your question it would be:
@phonecalls.each do |ph|
ph.questions.each do |question|
puts ph.id
puts question.id
end
end
Upvotes: 1