# Has many through & associate records

**URL:** <https://www.ruby-forum.com/t/has-many-through-associate-records/180933>\
**Category:** Rails\
**Created:** [December 31, 2009, 11:08pm UTC](https://www.ruby-forum.com/t/has-many-through-associate-records/180933 "2009-12-31T23:08:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jayoshi13](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jayoshi13](https://www.ruby-forum.com/u/jayoshi13)\
**Post date:** [December 31, 2009, 11:08pm UTC](https://www.ruby-forum.com/t/has-many-through-associate-records/180933/1 "2009-12-31T23:08:11Z")

</div>

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

–

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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![jayoshi13](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jayoshi13](https://www.ruby-forum.com/u/jayoshi13)\
**Post date:** [January 1, 2010, 6:31am UTC](https://www.ruby-forum.com/t/has-many-through-associate-records/180933/2 "2010-01-01T06:31:47Z")

</div>

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

On Jan 1, 3:06 am, jayoshi13 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Finding  
> assessment.  
> |  
> set of tests in the Findings table
> 
> Any help much appreciated!
> 
> Thanks

–

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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![jayoshi13](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jayoshi13](https://www.ruby-forum.com/u/jayoshi13)\
**Post date:** [January 1, 2010, 8:08am UTC](https://www.ruby-forum.com/t/has-many-through-associate-records/180933/3 "2010-01-01T08:08:56Z")

</div>

On Thu, Dec 31, 2009 at 2:06 PM, jayoshi13 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> class Finding  
> assessment.  
> |  
> set of tests in the Findings table
> 
> Any help much appreciated!
> 
> Thanks

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!

1. create an assessment

assessment = Assessment.create!

1. create a finding

finding = Finding.create!

1. associate a quiz and assessment to a finding

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

1. check the quiz side of the association

quiz.findings  
quiz.assessments

1. check the assessment side of the association

assessment.findings  
assessment.quizzes

1. check the join model of the association

finding.quiz  
finding.assessment

1. 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 D. with Rails  
3ed”  
by Dave T. 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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![jayoshi13](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jayoshi13](https://www.ruby-forum.com/u/jayoshi13)\
**Post date:** [January 3, 2010, 12:32am UTC](https://www.ruby-forum.com/t/has-many-through-associate-records/180933/4 "2010-01-03T00:32:26Z")

</div>

Thanks for the detailed response Conrad, it help me out greatly!

Regards,

Jayoshi

On Fri, Jan 1, 2010 at 7:07 AM, Conrad T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > has\_many :assessments, :through =\> :findings  
> > Assessment I want to be able populate the Findings table with all  
> > ±--------+
> 
> for the below explanation. Next, you should be able to do the following:
> 
> quiz.findings  
> finding.assessment
> 
> assessment
> 
> > “Ruby on Rails: Talk” group.  
> > You received this message because you are subscribed to the Google G.  
> > “Ruby on Rails: Talk” group.  
> > To post to this group, send email to removed\_email\_address@domain.invalid.  
> > To unsubscribe from this group, send email to  
> > removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > .  
> > For more options, visit this group at  
> > [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–

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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).
