0

In my controller, I'm trying to get all requests that are associated with each key, which are associated with some user.

class PendingsController < ApplicationController
  # GET /pendings
  # GET /pendings.json
  def index
    @pending_requests = current_user.keys.reduce do |key|
      key.requests.where(ready: false).to_a
    end

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @pending_requests }
    end
  end

However, at @pending_requests, "there is an undefined method 'requests' for []:Array"

There are multiple keys associated with some user, and multiple requests associated with some key.

In the debugger, the key.requests is recognized as a Mongoid relation, but still fails.

Why is this?

1
  • You might get better answers if you include your Model code as well. (minimum working example) Commented Dec 3, 2012 at 19:40

1 Answer 1

1

I guess you're misunderstanding the reduce usage. Try:

@pending_requests = current_user.keys.each_with_object([]) do |key, array|
  array.concat key.requests.where(ready: false).to_a
end
Sign up to request clarification or add additional context in comments.

Comments

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.