0

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?

3
  • You should. Are you getting any error ? Commented Feb 2, 2015 at 12:40
  • 1
    @Arup: I was getting an error as it was trying to find a (missing) field on the relation object itself, not on each item in the list Commented Feb 2, 2015 at 12:52
  • Ok.. Cool. Please next time add the exception too. Commented Feb 2, 2015 at 13:00

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

@WillTaylor how about this "in a minute"? :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.