# Query thru association

**URL:** <https://www.ruby-forum.com/t/query-thru-association/232961>\
**Category:** Rails\
**Created:** [November 7, 2014, 4:05am UTC](https://www.ruby-forum.com/t/query-thru-association/232961 "2014-11-07T04:05:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [November 7, 2014, 4:05am UTC](https://www.ruby-forum.com/t/query-thru-association/232961/1 "2014-11-07T04:05:33Z")

</div>

Hi,

I’m stuck trying to figure out how to do a query to find all the  
questions in a selected subject only.

My associations:

class Subject \< ActiveRecord::Base  
has\_many :books  
has\_many :questions  
has\_many :chapters, :through =\> :books  
has\_many :sections, :through =\> :chapters  
has\_many :subsections, :through =\> :sections  
has\_many :minisections, :through =\> :subsections

class Question \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :minisections

The query:  
@question\_list = Question.where(all the questions associated with the  
subject through the above associations???)

Any suggestions where to start would be appreciated.

Thanks,  
Dave

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [November 7, 2014, 9:52am UTC](https://www.ruby-forum.com/t/query-thru-association/232961/2 "2014-11-07T09:52:34Z")

</div>

On 7 November 2014 03:05, Dave C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> has\_many :chapters, :through =\> :books  
> has\_many :sections, :through =\> :chapters  
> has\_many :subsections, :through =\> :sections  
> has\_many :minisections, :through =\> :subsections
> 
> class Question \< ActiveRecord::Base  
> has\_and\_belongs\_to\_many :minisections
> 
> The query:  
> @question\_list = Question.where(all the questions associated with the  
> subject through the above associations???)

@question\_list = @subject.questions

Note, though, that since you have subject has\_many questions you also  
need question belongs\_to subject. Also you need to re-think your  
associations as you have two routes to question from subject -  
directly through the has\_many but also right through the chain of  
chapters, etc.

Colin

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [November 10, 2014, 9:01pm UTC](https://www.ruby-forum.com/t/query-thru-association/232961/3 "2014-11-10T21:01:40Z")

</div>

Colin L. wrote in post #1161913:

> On 7 November 2014 03:05, Dave C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > has\_many :chapters, :through =\> :books  
> > has\_many :sections, :through =\> :chapters  
> > has\_many :subsections, :through =\> :sections  
> > has\_many :minisections, :through =\> :subsections
> > 
> > class Question \< ActiveRecord::Base  
> > has\_and\_belongs\_to\_many :minisections
> > 
> > The query:  
> > @question\_list = Question.where(all the questions associated with the  
> > subject through the above associations???)
> 
> @question\_list = @subject.questions
> 
> Note, though, that since you have subject has\_many questions you also  
> need question belongs\_to subject. Also you need to re-think your  
> associations as you have two routes to question from subject -  
> directly through the has\_many but also right through the chain of  
> chapters, etc.
> 
> Colin

Thank you!

Dave
