Jump to content

The ultimate community for Ruby on Rails developers.


Photo

NameError

ruby rails nameerror mysql activerecord

  • Please log in to reply
2 replies to this topic

#1 kd2tv

kd2tv

    Passenger

  • Members
  • 3 posts

Posted 08 August 2014 - 11:01 PM

Can anyone help me with the error? 

2.1.0 :010 >   page.editors
NameError: undefined local variable or method `page' for main:Object
	from (irb):10
	from /Users/Kristjan/.rvm/gems/ruby-2.1.0/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
	from /Users/Kristjan/.rvm/gems/ruby-2.1.0/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
	from /Users/Kristjan/.rvm/gems/ruby-2.1.0/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
	from bin/rails:4:in `require'
	from bin/rails:4:in `<main>'
2.1.0 :011 > 

I am following a lynda.com series, so the code i have on the ruby files is the exact same as on the videos except i get an error and the instructor doesn't. 

 

here is the code 

class AdminUser < ActiveRecord::Base

  # To configure a different table name:
  # self.table_name = "admin_users"

  has_and_belongs_to_many :pages

end
class Page < ActiveRecord::Base

  belongs_to :subject
  has_many :sections
  has_and_belongs_to_many :editors, :class_name => "AdminUser"

end
class CreatePages < ActiveRecord::Migration

  def up
    create_table :pages do |t|
      t.integer "subject_id"
      # same as: t.references :subject
      t.string "name"
      t.string "permalink"
      t.integer "position"
      t.boolean "visible", :default => false
      t.timestamps
    end
    add_index("pages", "subject_id")
    add_index("pages", "permalink")
  end

  def down
    drop_table :pages
  end

end
class CreateAdminUsersPagesJoin < ActiveRecord::Migration

  def up
    create_table :admin_users_pages, :id => false do |t|
      t.integer "admin_user_id"
      t.integer "page_id"
    end
    add_index :admin_users_pages, ["admin_user_id", "page_id"]
  end

  def down
    drop_table :admin_users_pages
  end

end

Please help me with this error. I cant move on without fixing this.

 

Thank you.



#2 Ohm

Ohm

    Driver

  • Moderators
  • 401 posts
  • LocationCopenhagen

Posted 11 August 2014 - 05:26 AM

page doesn't exist in global scope, but Page does (capital P). Page is the class name. Use it like

Page.first.editors

to get the first page's editors.


Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk


#3 kd2tv

kd2tv

    Passenger

  • Members
  • 3 posts

Posted 22 August 2014 - 10:28 PM

page doesn't exist in global scope, but Page does (capital P). Page is the class name. Use it like

Page.first.editors

to get the first page's editors.

 

 

Thanks for the reply, but this did not work for me.







Also tagged with one or more of these keywords: ruby, rails, nameerror, mysql, activerecord

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users