has many through & associate records

3 views
Skip to first unread message

jayoshi13

unread,
Dec 31, 2009, 5:06:26 PM12/31/09
to Ruby on Rails: Talk
The app I'm building performs Assessments through a series of Tests
(about 120 tests in total), the results are stored in the join table
Findings.

Below is the has_many throughout association I am using -

class Test
has_many :findings
has_many :assessments, :through => :findings

class Finding
belongs_to :assessment
belongs_to :test

class Assessment
has_many :findings
has_many :tests, :through => :findings

The data in the Test table is primarily static. When I Create a new
Assessment I want to be able populate the Findings table with all
tests in the Test table, as all tests are mandatory as part of the
assessment.

I can get this to partially work, for example -

a = Assessment.create!(:name => 'assessment1', :test_ids => ['1', '2',
'3'])

mysql>select * from FINDINGS;
+----+---------------+----------------------+-------+----------
+---------+
| id | test_id | assessment_id | pass | fail | verdict
|
+----+---------------+----------------------+-------+----------
+---------+
| 1 | 1 | 1 | NULL | NULL |
NULL |
| 2 | 2 | 1 | NULL | NULL |
NULL |
| 3 | 3 | 1 | NULL | NULL |
NULL |

But I cannot figure out how to supply all Test id's to create the full
set of tests in the Findings table

Any help much appreciated!

Thanks

Piyush with Rails

unread,
Jan 1, 2010, 12:30:09 AM1/1/10
to Ruby on Rails: Talk
Hi Jayoshi,

But I cannot figure out how to supply all Test id's to
create the full
set of tests in the Findings table

Can you please specify what you need to populate in finding table. as
you have mentioned that you need to specify test ids in finding table
that is already get achieved with the way you followed to populate
findings table.

Please provide details.

Thanks,
Piyush

Conrad Taylor

unread,
Jan 1, 2010, 2:07:36 AM1/1/10
to rubyonra...@googlegroups.com
First, I would recommend not using "Test" because it happens to be
module name used by the Ruby Test::Unit.  Thus, let's use class Quiz
for the below explanation.  Next, you should be able to do the following:

1)  create a quiz

quiz = Quiz.create!

2)  create an assessment

assessment = Assessment.create!

3) create a finding

finding = Finding.create!

4)  associate a quiz and assessment to a finding

finding.quiz = quiz
finding.assessment = assessment
finding.save

5)  check the quiz side of the association

quiz.findings
quiz.assessments

6)  check the assessment side of the association

assessment.findings
assessment.quizzes

7)  check the join model of the association

finding.quiz
finding.assessment

8)  check the findings table

Finding.all

Next, in the above, one needed to create both ends of the association (i.e. assessment and quiz) for
the above process to work.  Next, if you are wanting to associate one or more quizzes to an
assessment, you can do one of the following:

assessment.quizzes << quiz1   # associates a single quiz to this assessment

or

Quiz.all.each { |item| assessment.quizzes << item }   # associates all the quizzes to this assessment

or

assessment.quizzes.create!  # create a quiz and associate it to this assessment

Lastly, I would recommend referencing "Agile Web Development with Rails 3ed" by Dave Thomas et al
because it covers this information in greater detail.

Good luck,

-Conrad






quiz.findings << finding

 
--

You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.



Jayoshi

unread,
Jan 2, 2010, 6:30:48 PM1/2/10
to rubyonra...@googlegroups.com
Thanks for the detailed response Conrad, it help me out greatly!

Regards,

Jayoshi
Reply all
Reply to author
Forward
0 new messages