0

So according to the tutorial on Railsspace, I am suppose to use the console to understand Active Records. So I do what that things says I should do and this is the result I get.

Does anyone know where the problem is?

   $ rails console

Output

Loading development environment (Rails 3.0.10)
ruby-1.9.2-p290 :001 > user = User.new(:screen_name => "me",
ruby-1.9.2-p290 :002 >     :email => "",
ruby-1.9.2-p290 :003 >     :password => "a",)
NameError: uninitialized constant User::SCREEN_NAME_RANGE
    from /Users/dennisbuizert/Sites/gpoff/app/models/user.rb:3:in `<class:User>'
    from /Users/dennisbuizert/Sites/gpoff/app/models/user.rb:1:in `<top (required)>'
    from (irb):1
    from /Users/dennisbuizert/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.10/lib/rails/commands/console.rb:44:in `start'
    from /Users/dennisbuizert/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.10/lib/rails/commands/console.rb:8:in `start'
    from /Users/dennisbuizert/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.10/lib/rails/commands.rb:23:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

User model

    class User < ActiveRecord::Base
  validates_uniqueness_of :screen_name, :email
  validates_length_of     :screen_name, :within => SCREEN_NAME_RANGE
  validates_length_of     :password,    :within => PASSWORD_RANGE
  validates_length_of     :email,       :maximum => EMAIL_MAX_LENGTH
  validates_presence_of   :email

  # Max & Min length for all fields
  SCREEN_NAME_MIN_LENGTH  = 3
  SCREEN_NAME_MAX_LENGTH  = 20
  PASSWORD_MIN_LENGTH     = 8
  PASSWORD_MAX_LENGTH     = 26
  EMAIL_MAX_LENGTH        = 50
  SCREEN_NAME_RANGE = SCREEN_NAME_MIN_LENGHT..SCREEN_NAME_MAX_LENGTH
  PASSWORD_RANGE = PASSWORD_MIN_LENGHT..PASSWORD_MAX_LENGTH

end

I am using SQLite3 rather than MySQL, because I cannot figure out how to replace SQLite3 with mysql and how to get that working.

1
  • Don't user SQLite3 when you want to be using MySQL just because you can't figure out how to use it. There are people (myself included) who would be happy to help you with that. As for your problem, could you post a little bit of your User model code?
    – Max
    Aug 28, 2011 at 12:14

3 Answers 3

4

I see where the error is coming from. You must declare your constants before your validations. When the class is loaded, the constant you reference in your validation have not been declared yet, hence the error.

Your code should look like this:

class User < ActiveRecord::Base

  # Max & Min length for all fields
  SCREEN_NAME_MIN_LENGTH  = 3
  SCREEN_NAME_MAX_LENGTH  = 20
  PASSWORD_MIN_LENGTH     = 8
  PASSWORD_MAX_LENGTH     = 26
  EMAIL_MAX_LENGTH        = 50
  SCREEN_NAME_RANGE = SCREEN_NAME_MIN_LENGHT..SCREEN_NAME_MAX_LENGTH
  PASSWORD_RANGE = PASSWORD_MIN_LENGHT..PASSWORD_MAX_LENGTH

  validates_uniqueness_of :screen_name, :email
  validates_length_of     :screen_name, :within => SCREEN_NAME_RANGE
  validates_length_of     :password,    :within => PASSWORD_RANGE
  validates_length_of     :email,       :maximum => EMAIL_MAX_LENGTH
  validates_presence_of   :email

end
4
  • 1
    It didn't change anything sadly. Still producing the error in console.
    – dbuizert
    Aug 28, 2011 at 13:58
  • It produces the same error with the same stack trace? I am 99% positive your ordering was a bug, so I feel like this may be a new a bug.
    – Max
    Aug 28, 2011 at 14:13
  • ActiveRecord::UnknownAttributeError: unknown attribute: screen_name That was the error it produced after fixing the text errors... I had several T's and H's switched.
    – dbuizert
    Aug 28, 2011 at 14:28
  • Could you please edit the question with the new stack trace and I will do my best to edit my response to help you.
    – Max
    Aug 28, 2011 at 22:34
0

To get sqlite working, make sure you have the line gem sqlite3 in your Gemfile, run bundle install, and make your sections in config/database.yml look something like this:

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

As for the SCREEN_NAME_RANGE error, I can't tell where that's coming from without seeing your code (assuming that the above config changes don't fix it). If you're absolutely sure your files are exactly like they're shown in the tutorial, I'd contact the author since it's a non-free doc set.

You might want to check out http://guides.rubyonrails.org/ . They have big warnings that they're incomplete, but for the most part they're pretty well-written (and by publicly active contributing members of the community), and free.

1
  • 1
    Sqlite3 is installed, it did that by default when it installed everything. Didn't fix the problem though.
    – dbuizert
    Aug 28, 2011 at 14:26
0

this problem occurs when editing model files while autotest is running

So just stop autotest then start it again (if you want)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.