Will Taylor
Will Taylor

Reputation: 2304

Rails: Join arrays from two queries

I'm sorry to ask a stupid question. I've got an array that I want to populate with two similar queries to my database.

@experience_items = @user.experience_items.where(current: true).order(start_date: :desc)
@experience_items << @user.experience_items.where(current: false).order(end_date: :desc)

This is currently returning an ActiveRecord::AssociationRelation, which I can't iterate through with <% @experience_items.each do |item| %>

I know this is basic, but I just don't understand it. Why can't I iterate through @experience_items?

Upvotes: 0

Views: 43

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51171

It's because you add ActiveRecord::Relation to @experience_items as an element instead of concatenating these array-like objects. This should work:

@experience_items += @user.experience_items.where(current: false).order(end_date: :desc)

Upvotes: 1

Related Questions