ineedahero
ineedahero

Reputation: 537

Rails Controller: How to return list of specific objects?

I want to set my @users variable to a specific list of users (whose id's are specified in a file).

This is my controller:

class UsersController < ApplicationController
unloadable
require 'will_paginate/array'

ENTRIES_PER_PAGE = 15

def index
    if some_condition
      @users = User.all
    ...
    ...
    ...

    /* inside else block */
    ids = []
    File.open("./user_ids.txt", "r") do |f|
      f.each_line do |line|
        ids.push[line]
      end
    end

    /* ??? */
#   @users = User.find(ids)
    /* When above line is uncommented, application throws error */

The commented line throws an error, I think because @users isn't an array, but something else, and I'm not sure what I'm supposed to do.

ActionView::Template::Error (undefined method `total_entries' for #<Array:0x000000044aa688>)

total_entries is defined from the will_paginate gem

<p> Total entries: <%= @users.total_entries %> </p>

Upvotes: 0

Views: 594

Answers (1)

Said Kaldybaev
Said Kaldybaev

Reputation: 10002

that's because when you do User.find(ids) it will return an array, just do User.where(id: ids).paginate(page: params[:page])

Upvotes: 1

Related Questions