简体   繁体   中英

Merge two Arrays and sort by created_at Video and Images

I'm new to Ruby so I trying hard to do this,I'm trying to unite these two arrays to have videos and images mixed by date, the way it is, the shots will always be on top of the videos.

In the controller I have

@videos = Video.all.order("created_at desc")
@shots = Shot.all.order("created_at desc")

and in the view

   <% @shots.each do | shot| %>
    <%= render shot %>
   <% end %>

  <% @videos.each do | shot| %>
    <%= render video %>
   <% end %>

how do I put these two arrays together in order of time, to have shots and videos together?

Try it like this, for instance:

Controller

@resources = Video.order(created_at: :desc) | Shot.order(created_at: :desc)

View

<%= render @resources %>

You should then have a partial named _resource.html.erb in that case

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM