# AR query question

**URL:** <https://www.ruby-forum.com/t/ar-query-question/186615>\
**Category:** Rails\
**Created:** [April 9, 2010, 8:02pm UTC](https://www.ruby-forum.com/t/ar-query-question/186615 "2010-04-09T20:02:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [April 9, 2010, 8:02pm UTC](https://www.ruby-forum.com/t/ar-query-question/186615/1 "2010-04-09T20:02:08Z")

</div>

Here’s how my associations map:

class Journal \< ActiveRecord::Base  
has\_many :music\_journal\_entries  
has\_many :film\_journal\_entries  
end

class FilmJournalEntry \< ActiveRecord::Base  
belongs\_to :journal  
end

class MusicJournalEntry \< ActiveRecord::Base  
belongs\_to :journal  
end

Both “entries” tables have correct foreign keys set-up and contain  
records that map correctly when querying their values through  
Journal.

I just want to be able to pull in all of the film\_journal\_entries and  
music\_journal\_entries buy running an AR query through Journal.

I have tried things like:  
all\_entries = Journal.all :joins =\>  
[:music\_journal\_entries, :film\_journal\_entries]  
and some other things utilizing :include, but I still haven’t got it.

How would I do this?  
Thanks!!

Elliott G

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [April 9, 2010, 8:41pm UTC](https://www.ruby-forum.com/t/ar-query-question/186615/2 "2010-04-09T20:41:56Z")

</div>

On 9 April 2010 19:00, elliottg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Both “entries” tables have correct foreign keys set-up and contain  
> records that map correctly when querying their values through  
> Journal.
> 
> I just want to be able to pull in all of the film\_journal\_entries and  
> music\_journal\_entries buy running an AR query through Journal.

The easiest way is just:

all\_entries = music\_journal\_entries + film\_journal\_entries

But it might be worth remodelling your DB a little. It looks to me  
like a good candidate for STI to have a single “journal\_entries”  
table, and then a type of MusicJournalEntry and a type of  
FilmJournalEntry (of course, if they’ve got loads of different  
properties, you might not want to use STI, but maybe a polymorphic  
model would help instead).  
That would keep all of a journal’s entries in one table:

class JournalEntry \< ActiveRecord::Base  
belongs\_to :journal  
end

class FilmJournalEntry \< JournalEntry  
end

class MusicJournalEntry \< JournalEntry  
end

All you need to do to support this is to ensure there’s a string field  
in the “journal\_entries” table called “type” - Rails handles the rest.

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [April 9, 2010, 9:38pm UTC](https://www.ruby-forum.com/t/ar-query-question/186615/3 "2010-04-09T21:38:09Z")

</div>

Thanks for the input Michael.

I’ll rethink my data structure a bit.

EG

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [April 10, 2010, 2:56am UTC](https://www.ruby-forum.com/t/ar-query-question/186615/4 "2010-04-10T02:56:42Z")

</div>

Elliott G. wrote:

> Thanks for the input Michael.
> 
> I’ll rethink my data structure a bit.
> 
> EG

Your could use Single table inheritance or polymorphic set up if you are  
concerned about table getting too large in inheritance…
