Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8039425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:33:56+00:00 2026-06-05T03:33:56+00:00

Below I created a class method in a model called Movie that should return

  • 0

Below I created a class method in a model called Movie that should return an array:

def self.all_ratings
  Array['G','PG','PG-13','R','NC-17']
end

And in my movies controller I access it using the following instance variable:

@all_ratings = Movie.all_ratings

However when it comes time to use it in my index view I receive the following errors:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

I believe I am creating the array properly but I could be wrong. Any suggestions why these errors occur?

Below is the view where @all_ratings is used:

%h1 All Movies

= form_tag movies_path, :method => :get do
  Include: 
  - @all_ratings.each do |rating|
    = rating
    = check_box_tag "ratings[#{rating}]"
  = submit_tag 'Refresh'

And here is how I implemented @all_ratings into the controller

class MoviesController < ApplicationController

  @all_ratings = Movie.all_ratings
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:33:57+00:00Added an answer on June 5, 2026 at 3:33 am

    The code to initialize an instance variable needs to be in an instance method.

    (Otherwise the scope is the class, not an instance.)

    class MoviesController < ApplicationController
      def index # Or wherever
        @all_ratings = Movie.all_ratings
      end
    end
    

    If you need that value in several methods, you could use, say, a before_filter.

    Ruby is different from languages like, say, Java. In Java, instance variables are defined outside instance methods, and are available in every instance method, with whatever value they were initialized with.

    In Ruby, there are two (major) ways to handle instance variables: use a method like attr_accessor to create accessor methods, or initialize them inside an instance method, as shown above.

    Once an instance variable has initialized, its value is usable from any other instance method. For example, in your comments you mention initializing it in a ratings method. Unless ratings is explicitly called, @all_ratings will not be initialized. In other words, if you make a GET request to index, the ratings method will not be called, and @all_ratings will still be nil.

    If you explicitly call ratings from index, then @all_ratings will be initialized (by the ratings method). Once it’s initialized in any instance method, that instance of the object (the controller in this case) has an initialized @all_ratings instance variable:

    def index
      ratings
    end
    

    Now the value of @all_ratings is available in index’s template.

    Without putting the instance variable initialization in an instance method what you’re actually doing is creating an instance variable in the class Foo, which is something very different:

    [1] pry(main)> class Foo
    [1] pry(main)*   @wat = self.class
    [1] pry(main)* end  
    => Class
    [2] pry(main)> f = Foo.new
    => #<Foo:0x007fbfba8efba8>
    [5] pry(main)> f.instance_variables
    => []
    [6] pry(main)> Foo.instance_variables
    => [:@wat]
    [7] pry(main)> Foo.instance_eval "@wat"
    => Class
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a created a class called Animal as shown below: class Animal{ int eyes,legs;
A method I work with that is called tens of thousands of times started
I have created a class in my asp.net MVC 3 application's Model folder and
I would like to create a JTable like the below picture: Which java class
//I have created below snippet to let the sensor to be detected. -(void)addProximitySensorControl {
I've refactored how the merged-dictionary ( all_classes ) below is created, but I'm wondering
I created a rotating cube using the below code and add bitmaps as sides
I have created one table using the below command: create table Table1( Id int
I created few user defined types in my database as below CREATE TYPE [dbo].[StringID]
I have created button 2 below: <input id=Button1 type=button value=Stop onclick=alert('hello world');/> <input id=Button2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.