Nanni
Nanni

Reputation: 5

select from 2 tables and order them in rails

i want to select some rows from different tables and then i want to order them. how can i merge these results and order them according to their created_at values?

@books = Book.all
@notebooks = Notebook.all

@booksandnotebooks = ????

Upvotes: 0

Views: 338

Answers (1)

Max Williams
Max Williams

Reputation: 32955

@booksandnotebooks = (@books + @notebooks).sort_by(&:created_at)

Isn't ruby lovely :)

Upvotes: 1

Related Questions