1

I'm going through Agile Web Development With Rails 4 and have got to the point where I create a user. It's being done like so:

rails generate scaffold User name:string password:digest

But when I try to run rake db:migrate I get the following error: undefined method digest'

What is the cause of that and how can I fix it? I googled that digest is a rails 4 feature, so how come it's an issue?

Taryn
  • 242,637
  • 56
  • 362
  • 405
Xeen
  • 6,955
  • 16
  • 60
  • 111
  • Read this [rails database column types](http://stackoverflow.com/questions/11889048/is-there-documentation-for-the-rails-column-types) – Roman Kiselenko Dec 07 '13 at 18:55
  • This generator syntax did make it into 4.0, see [this commit](https://github.com/rails/rails/commit/3008994d1e29b7e59a64bf0a03b5408a2946db25). It doesn't seem to be documented anywhere else though. – Inkling Jul 24 '14 at 06:31

1 Answers1

0

Edit:

The information below is not entirely correct. It appears as if this generator feature is poorly documented, but does exist. Using password:digest in the generator simply creates a string column in the migration called :password_digest and adds has_secure_password to the model. A tutorial on this feature can be found here.


I see no mention of being able to define a password:digest in the Rails 4 Migrations Guide. Nor do I see it in the actual Rails 4 stable source.

You are correct that this page mentions it as being something you can do, however, it appears as if this feature either got dropped or has not made it into Rails 4 stable yet.

As far as fixing it, it appears that in Rails 4 stable these are the fields you can define with a migration:

t.column
t.index
t.rename_index
t.timestamps
t.change
t.change_default
t.rename
t.references
t.belongs_to
t.string
t.text
t.integer
t.float
t.decimal
t.datetime
t.timestamp
t.time
t.date
t.binary
t.boolean
t.remove
t.remove_references
t.remove_belongs_to
t.remove_index
t.remove_timestamps

You will probably have to store the password as a string (but don't mistake this as me saying "store the plain text password as a string" (!) ... you still want to at least hash it and probably salt it.) This video should provide a lot of help with doing this in Rails: Authentication in Rails.

Authentication is a complex issue, and may require some research to do it properly, but it will be well worth it. I would highly recommend first watching that video and getting a basic understanding of user auth in Rails, and then possibly looking into some hardened gems that you can use. Using an existing gem has both the benefit of being fully-featured and also being battle-hardened by the open source community. If you find the right gem that fits your needs and is well reviewed and actively maintained, it will usually be better than what you can build yourself.

Joe Edgar
  • 872
  • 5
  • 13
  • Thank you for explanation, but it eventually did work out when I switched database to `sqlite`. A bit weird though and I'm not sure I want to use a function that's not even mentioned in the official documentation. – Xeen Dec 08 '13 at 09:23
  • Ah, okay. I see now. I was looking in the wrong place in the code. When you run this generator it does, indeed, simply create a migration using "t.string :password_digest" and then automatically puts has_secure_password in the user model. So the code for this is not reflected in the migration but in the generator. There is some information on this [here](http://www.millwoodonline.co.uk/blog/using-hassecurepassword-for-rails-authentication). – Joe Edgar Dec 08 '13 at 16:37
  • I wouldn't worry too much about using this feature. It is documented [here](http://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html). It appears as if it has been a part of rails since 3.1. There are a couple of good articles on this. [This one](http://vikinghammer.com/2013/02/20/rails-has-secure-password-reminder/) goes well with the video I mentioned in my post. I would highly recommend checking it out. What the generator does (when you include password:digest) is simply set up the migration and model for you to be able to use this feature. – Joe Edgar Dec 08 '13 at 16:43