Welcome to the Treehouse Community
Looking to learn something new?
undefined method 'first_name' for #<...>
After reading the course "Migrating statuses". Please i need some help about this error : undefined method 'first_name' for #<...>
I tried several commands but nothing changes : rake db:reset rake db:migrate
Any idea ?
Thanks in advance
I'm enjoying it but shame I couldn't get a quicker resolution so you can get on!
Well at this point I would look at the database table - I am assuming you're using the default database which is SQLite. I don't know how that is done with SQLite (I only use Postgres).
However, what the migrations are saying you have a first_name but the console is saying you don't have a first_name. I know of two reasons for this.
- You have reset the server (control + C) - highly unlikely after a debugging session of this length!
- The migrations may have been run before a change was made to them (once you have run them, editing them later on and running migration will not change the database).
I would delete the database, create it again and run the migration. So stop the webserver (control + C or whatever it is on your machine) then:
rake db:drop rake db:create rake db:migrate rake db:test:prepare
Then test the console (rails console) and check your migrations have run
user = User.new user.first_name rake db:migrate:status
If that passes run the tests and then try out the application. Haha... fingers crossed and good luck :-)
For these problems you need to give more of the full error message (at least the first 10 lines) and what you are doing when it happens - running the application or running the test.
OK. You've just watched Creating an authentication System / migrating Statuses
The error is in the new so it shouldn't be anything to do with old records in the database. To confirm the situation. Go into the root of your treehouse project. type:
Within the console type:
user = User.new user.first_name
This should reproduce your error:
NoMethodError: undefined method `first_name' for #<User:0xbd4303d8> from /home/richard/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/activemodel-4.0.0.rc2/lib/active_model /attribute_methods.rb:436:in `method_missing'
If that's the case then the next thing you want to do is check the migration add_user_id_to_status. The migration should be under the db/migrate directory and the actual file begins with a lot of numbers representing the date and time it was created: treehouse/db/migrate/201305[some numbers]add_user_id_to_status.rb
class AddUserIdToStatuses < ActiveRecord::Migration def change add_column :statuses, :user_id, :integer add_index :statuses, :user_id remove_column :statuses, :name end end
Does your migration look like this?
By confirm... I assume you mean that:
- The console gave the same error
- The migration is the same
If that is the case... from the root of your treehouse project type:
Copy and paste the result - we want to see if "Add user id to statuses" is present or not. It should look something like this:
database: treehouse_development Status Migration ID Migration Name -------------------------------------------------- up 20130612134026 Create statuses up 20130613125801 Devise create users up 20130614085711 Add user id to statuses
Actually this sounds good. The status command was to see if the migration had been run or not. It has - so we know the migrations are being run as we expected this is your current state:
Status Migration ID Migration Name up 20130609171415 Create statuses up 20130617191642 Devise create users up 20130617200718 Add user id to statuses
Next, I would check the devise_create_users migration file. During Generating the user Model they add first_name, last_name and profile_name - you should check that the start of DeviseCreateUsers looks like this:
class DeviseCreateUsers < ActiveRecord::Migration def change create_table(:users) do |t| ## Database authenticatable t.string :first_name <==== Is this here? t.string :last_name <==== Is this here? t.string :profile_name <==== Is this here? t.string :email, :null => false, :default => "" t.string :encrypted_password, :null => false, :default => "" .... ....
Is this change missing? If it is you will have to do rake db:rollback two times (one for each migration you are changing) and then rake db:migrate 1 time (it runs all the pending migrations at once).
Reference Rails Guide on Migrations
Looks like we're good! The original error was:
- Status#first_name (which sounds odd).
Now it is
- Status#name is missing.
This is an expected error! In the Migrating Status Video you remove the Status#name field which breaks the code in the views. You remove the name with the migration written in this video - AddUserIdToStatuses:
class AddUserIdToStatuses < ActiveRecord::Migration def change add_column :statuses, :user_id, :integer add_index :statuses, :user_id remove_column :statuses, :name <=== During the video you remove the column. end Views are not updated till the next video! end
However, as you are seeing the name field is a problem when you're running the program since the Status views expect Status#name to be present. In the video after the migration one.. Getting the latest Changes ... Jim fixes this exact problem.
I think we've made it Theirry! * manic laugh * :-) But I'll need you to confirm first ;-)