# Displaying two different data sets at the same time

**URL:** <https://www.ruby-forum.com/t/displaying-two-different-data-sets-at-the-same-time/147049>\
**Category:** Rails\
**Created:** [August 28, 2008, 12:23am UTC](https://www.ruby-forum.com/t/displaying-two-different-data-sets-at-the-same-time/147049 "2008-08-28T00:23:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elliotchyba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8dc957/32.png) [@elliotchyba](https://www.ruby-forum.com/u/elliotchyba)\
**Post date:** [August 28, 2008, 12:23am UTC](https://www.ruby-forum.com/t/displaying-two-different-data-sets-at-the-same-time/147049/1 "2008-08-28T00:23:02Z")

</div>

Thanks to some help from earlier posters, I’ve been moving along with my  
test application. I did run into one more stumbling block, though.

Say I have two sets of headlines, news and sports, the table structures  
are slightly different but each has a headline, writer and date.

In the controller:  
@news = News.find(:all)  
@sports = Sports.find(:all)

Getting they to display like this isn’t a problem.

News  
News Headline 1 - Writer 08-01-2008  
News Headline 2- Writer 07-31-2008  
News Headline 3- Writer 06-05-2008

Sports  
Sports Headline 1- Writer 07-30-2008  
Sports Headline 2- Writer 06-16-2008

My question is this:  
How do you structure the loop to go through both arrays and display  
based on date so that the result could be two, intertwined lists.  
Example:

News Headline 1 - Writer 08-01-2008  
News Headline 2 - Writer 07-31-2008  
Sports Headline 1 - Writer 07-30-2008  
Sports Headline 2 - Writer 06-16-2008  
News Headline 3 - Writer 06-05-2008

Any help or starting points would be sincerely appreciated.

---

<div class="post-metadata">

**Author:** ![elliotchyba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8dc957/32.png) [@elliotchyba](https://www.ruby-forum.com/u/elliotchyba)\
**Post date:** [August 28, 2008, 5:10am UTC](https://www.ruby-forum.com/t/displaying-two-different-data-sets-at-the-same-time/147049/2 "2008-08-28T05:10:30Z")

</div>

Elliot C. wrote:

> My question is this:  
> How do you structure the loop to go through both arrays and display  
> based on date so that the result could be two, intertwined lists.  
> Example:
> 
> News Headline 1 - Writer 08-01-2008  
> News Headline 2 - Writer 07-31-2008  
> Sports Headline 1 - Writer 07-30-2008  
> Sports Headline 2 - Writer 06-16-2008  
> News Headline 3 - Writer 06-05-2008
> 
> Any help or starting points would be sincerely appreciated.

you could use polymorphic associations?  
[http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html](http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html)

your class definitions might look lke  
class Article \< ActiveRecord::Base  
belongs\_to :content, :polymorphic =\>  
end  
class News \< ActiveRecord::Base  
has\_one :article, as =\> :content  
end  
class Sports \< ActiveRecord::Base  
has\_one :article, as =\> :content  
end

Also, refer to Polymorphic Associations section in Chapter 18.4 Joining  
to Multiple Tables of Agile Web D. with Rails (third edition).  
If you dont have this book, i HIGHLY recommend you get it from

> **[Pragmatic Bookshelf: By Developers, For Developers](https://pragprog.com)**
>
> We improve the lives of professional developers. We create timely, practical books on classic and cutting-edge topics to help you learn and practice your craft, and accelerate your career. Come learn with us.

---

<div class="post-metadata">

**Author:** ![elliotchyba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8dc957/32.png) [@elliotchyba](https://www.ruby-forum.com/u/elliotchyba)\
**Post date:** [August 28, 2008, 5:11am UTC](https://www.ruby-forum.com/t/displaying-two-different-data-sets-at-the-same-time/147049/3 "2008-08-28T05:11:34Z")

</div>

aa aa wrote:  
slight typo, missed the "true

> class Article \< ActiveRecord::Base  
> belongs\_to :content, :polymorphic =\> true  
> end  
> class News \< ActiveRecord::Base  
> has\_one :article, as =\> :content  
> end  
> class Sports \< ActiveRecord::Base  
> has\_one :article, as =\> :content  
> end
