anonymftw
anonymftw

Reputation: 7

How does a controller class access a model class?

How does this random controller class

class RandomController < ApplicationController
  def index
    @user = User.all
  end
end

access the User class? I've been searching for the connection in the source files, but I can't seem to find a logical explanation.

Upvotes: 0

Views: 44

Answers (1)

Phlip
Phlip

Reputation: 5343

Rails has 'constant autoloading', so you don't need to add require 'user' to the top of your file.

http://guides.rubyonrails.org/autoloading_and_reloading_constants.html

When Rails encounters a missing constant, it tries to load a file with a filename based on the constant's name. This nearly always works smoothly... C-;

Upvotes: 5

Related Questions