Show data from multiple Items?

Hi, So after completing yelp and etsy demo, I am creating a new app. In that I have 4 things Quotes, Images, Facts, Jokes. Each thing have its own controller, model file. So if I want to see quotes submitted by user I can go to localhost:3000/quotes and same with other things. Now What I want is show all this 4 things on one page. All 4 things should be displayed in order they was created. How can I do this? Thanks. Sumeet

Posted about 4 years ago by Sumeet
Posted about 4 years ago by Alex Yang

If I'm understanding correctly, you want to display 'things' in chronological order, regardless of what kind of 'thing' it is (e.g. quote, image, fact, joke). If that's right, unfortunately I can't think of an elegant way to do this. The reason is because each of these things have different attributes. For Quotes, you'll probably want to display a text field. For Images, you would need to display an image, and so on.

A potential workaround would be to define a variable (e.g. '@things') in your controller that would retrieve all the Quotes, Images, Facts, and Jokes individually, then combine them into a single array ordered by their 'created_at' attribute. Then in your view, you would need to use an if-elsif-else statement to check what type of object the 'thing' is. Depending on whether it's a Quote, Image, Fact, or Joke, you would then decide how to display it on the page.

I found a Stack Overflow post that might be helpful to you. Again, it's an inelegant solution, but it should work. Check it out:
http://stackoverflow.com/questions/14341811/combining-multiple-models-into-1-view-rails

2
Posted about 4 years ago by Sumeet

Hi, its working now. I fixed the problem. Thank you.

1
Posted about 4 years ago by Alex Yang

Glad it's working now!

1
Posted about 4 years ago by Sumeet

Hey, Thanks for the answer. I am trying to implement it but getting some errors. Can you pls look at the code and check the code for errors? In the below code I am trying to add the 2 things only later when it works I will add other 2 also.

In controller file, I have added the following code.

def index
@quotes = Quote.all.order("created_at DESC")
@images = Image.all.order("created_at DESC")
@jokes = Joke.all.order("created_at DESC")

@items = (@quotes.to_a + @jokes.to_a)
end

Next in the index file I have added the below code.

<% if @items.any? %>

<div class="col-md-12">
<% @items.each.do |item| %>
<% if item.is_a? Quote %>

<div class="postbg">

<%= quote.quotefie %>
<div class="wedate pull-right wehi">
<%= quote.created_at.strftime("%b %d, %Y") %>
</div>
<div class="clearfix"></div>
<em class="pull-right wehi" style="margin-top:20px;"> - <%= quote.author %></em>
<%= link_to 'Show', quote %>
<%= link_to 'Edit', edit_quote_path(quote) %>
<%= link_to 'Destroy', quote, method: :delete, data: { confirm: 'Are you sure?' } %>
</div>
<% else %>

<% @jokes.each do |joke| %>

<div class="postbg">

<%= joke.jokefie %>
<div class="wedate pull-right wehi">
<%= joke.created_at.strftime("%b %d, %Y") %>
</div>
<div class="clearfix"></div>
<%= link_to 'Show', joke %>
<%= link_to 'Edit', edit_joke_path(joke) %>
<%= link_to 'Destroy', joke, method: :delete, data: { confirm: 'Are you sure?' } %>
</div>

<% end %>
<% end %>
</div>

<% end %>

1