AlexEfremo
AlexEfremo

Reputation: 813

Rails uninitialized constant error in command line

I'm using Rails 4. I created a db migration. Called AddPlaceidToUserid:

class AddPlaceidToUserid < ActiveRecord::Migration
  def change
    create_table :subscriptions do |t|
      t.integer :placeid
      t.integer :userid
    end
  end
end

After migration with rake db:migrate it creates a table in my database, but in rails console when I show all tables it shows me ["schema_migrations", "places", "subscriptions"], but when I try to show Subscriptions table console writes me an error:

2.0.0p353 :004 > Subscription.all
  NameError: uninitialized constant Subscription
    from (irb):4
    from /home/alexefremo/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.1/lib/rails/commands/console.rb:90:in `start'
    from /home/alexefremo/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.1/lib/rails/commands/console.rb:9:in `start'
    from /home/alexefremo/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.1/lib/rails/commands.rb:62:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

In PlacesController Subscription.all of course causes the same error. I missed something, except creating migration?

Upvotes: 0

Views: 899

Answers (1)

NARKOZ
NARKOZ

Reputation: 27911

What you need to do is to generate a model:

rails g model Subscription place_id:integer user_id:integer

That will create a model Subscription and also a database migration for it.

Upvotes: 3

Related Questions