Nick
Nick

Reputation: 3134

Why can't I display all database results in a rails index view?

I am stuck in Chapter 9 of the Rails tutorial. Here is my controller:

class UsersController < ApplicationController
before_action :signed_in_user, only: [:index, :edit, :update]
before_action :correct_user, only: [:edit, :update]

def index
@users = User.all
end

def show
@user = User.find(params[:id])
end

def new
@user = User.new
end

def create
@user = User.new(user_params) #@user = User.new(params[:user])
if @user.save
  flash[:success] = "Welcome to the Sample App!"
  redirect_to @user
else
  render 'new'
end
end

def edit
#not needed anymore due to before_action: @user = User.find(params[:id])
end

def update
# not needed anymore: @user = User.find(params[:id])
if @user.update_attributes(user_params)
  # Handle a successful update:
  flash[:success] = "Profile updated"
  redirect_to @user
else
  render 'edit'
end
end

private
def user_params
  params.require(:user).permit(:name, :email, :password, :password_confirmation)
end

# Before filters

def signed_in_user
  store_location
  redirect_to signin_url, notice: "Please sign in" unless signed_in?
end

def correct_user
  @user = User.find(params[:id])
  redirect_to(root_url) unless current_user?(@user)
end
end

Here is my view (users/index.html.erb):

<% provide(:title, 'All users') %>
<h1>All users</h1>

<ul class="users">
 <% Rails.logger.debug "users/index: @users.all.count = "[email protected]_s  %>
 <% @users.each do |user| %>
  <li>
  <%= gravatar_for user, size: 52 %>
  <%= link_to user.name, user %>
  </li>
 <% end %>
</ul>

The view shall display a list of users from the database. There are 100 of them:

2.0.0-p451 :003 > User.count
(2.4ms)  SELECT COUNT(*) FROM "users"
D, [2014-05-28T15:58:46.426154 #85338] DEBUG -- :    (2.4ms)  SELECT COUNT(*) FROM "users"
=> 100 

Yet, the view cannot find them. It finds a single user (which was the case before I entered 99 users via a rake task). Here is the server log:

Started GET "/users" for 127.0.0.1 at 2014-05-28 17:07:32 +0100
Processing by UsersController#index as HTML
User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'cd96a5eb437ae73a82b73e2f2ae20de90dad6da7' LIMIT 1
DEPRECATION WARNING: Relation#all is deprecated. If you want to eager-load a relation, you can call #load (e.g. `Post.where(published: true).load`). If you want to get an array of records from a relation, you can call #to_a (e.g. `Post.where(published: true).to_a`). (called from _app_views_users_index_html_erb__4537893108263142400_70344304651460 at /Users/nnikolo/Documents/private/rails_projects/sample_app/app/views/users/index.html.erb:5)
User Load (0.1ms)  SELECT "users".* FROM "users"
users/index: @users.all.count = 1
CACHE (0.0ms)  SELECT "users".* FROM "users"
Rendered users/index.html.erb within layouts/application (2.0ms)
Rendered layouts/_shim.html.erb (0.0ms)
Rendered layouts/_header.html.erb (0.5ms)
Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 15ms (Views: 14.0ms | ActiveRecord: 0.3ms)

I would suspect some sort of caching but according to the log only the second query SELECT "users".* FROM "users" was cached. The first one (done for the purposes of debugging) seems to have been served from the database, rather than the cache. Yet it finds only a single record (this is the line users/index: @users.all.count = 1). What am I doing wrong? I think I followed the instructions accurately.

Upvotes: 0

Views: 108

Answers (1)

Nick
Nick

Reputation: 3134

I just re-started the WEBrick server and now the query works as expected:

W, [2014-05-28T18:27:34.485654 #91837]  WARN -- : DEPRECATION WARNING: Relation#all is deprecated. If you want to eager-load a relation, you can call #load (e.g. `Post.where(published: true).load`). If you want to get an array of records from a relation, you can call #to_a (e.g. `Post.where(published: true).to_a`). (called from _app_views_users_index_html_erb___3901190429481931463_70221243438200 at /Users/nnikolo/Documents/private/rails_projects/sample_app/app/views/users/index.html.erb:5)
D, [2014-05-28T18:27:34.486359 #91837] DEBUG -- :   User Load (0.4ms)  SELECT "users".* FROM "users" 
D, [2014-05-28T18:27:34.486387 #91837] DEBUG -- :   User Load (0.4ms)  SELECT "users".* FROM "users"
D, [2014-05-28T18:27:34.519286 #91837] DEBUG -- : users/index: @users.all.count = 100
D, [2014-05-28T18:27:34.519349 #91837] DEBUG -- : users/index: @users.all.count = 100
D, [2014-05-28T18:27:34.519619 #91837] DEBUG -- :   CACHE (0.0ms)  SELECT "users".* FROM "users"
D, [2014-05-28T18:27:34.519647 #91837] DEBUG -- :   CACHE (0.0ms)  SELECT "users".* FROM "users"
I, [2014-05-28T18:27:34.537062 #91837]  INFO -- :   Rendered users/index.html.erb within layouts/application (52.0ms)
I, [2014-05-28T18:27:34.537116 #91837]  INFO -- :   Rendered users/index.html.erb within layouts/application (52.0ms)
I, [2014-05-28T18:27:34.545021 #91837]  INFO -- :   Rendered layouts/_shim.html.erb (0.0ms)
I, [2014-05-28T18:27:34.545065 #91837]  INFO -- :   Rendered layouts/_shim.html.erb (0.0ms)
I, [2014-05-28T18:27:34.546015 #91837]  INFO -- :   Rendered layouts/_header.html.erb (0.5ms)
I, [2014-05-28T18:27:34.546043 #91837]  INFO -- :   Rendered layouts/_header.html.erb (0.5ms)
I, [2014-05-28T18:27:34.546629 #91837]  INFO -- :   Rendered layouts/_footer.html.erb (0.1ms)
I, [2014-05-28T18:27:34.546655 #91837]  INFO -- :   Rendered layouts/_footer.html.erb (0.1ms)
I, [2014-05-28T18:27:34.547216 #91837]  INFO -- : Completed 200 OK in 65ms (Views: 63.1ms | ActiveRecord: 0.6ms)
I, [2014-05-28T18:27:34.547243 #91837]  INFO -- : Completed 200 OK in 65ms (Views: 63.1ms | ActiveRecord: 0.6ms)

My question is why do I have to re-start the server? The tutorial does not mention at all that this needs to be done (this is specifically Listing 9.27). I also don't remember changing the database structure since the last server re-start. I am a rails newbie but to me it sounds profoundly wrong if the server needs to be re-started after each insert into the database.

Upvotes: 1

Related Questions