Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Thierry MELLERAUD DE VILLARS
414 Pointsundefined method 'first_name' for #<...>
Hello,
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
6 Answers

Richard Wigley
3,733 PointsI'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 :-)
Rich

Richard Wigley
3,733 PointsFor 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.
Rich

Thierry MELLERAUD DE VILLARS
414 PointsYes, I get the following error :
NoMethodError in Statuses#new
Showing /Users/tmelleraud/treebook/app/views/statuses/_form.html.erb where line #15 raised:
undefined method `first_name' for #<Status:0x007f8303e1e7f8> Extracted source (around line #15):
12: <% end %> 13: 14: <div><%= f.label :first_name %><br /> 15: <%= f.text_field :first_name %></div> 16: 17: <div><%= f.label :last_name %><br /> 18: <%= f.text_field :last_name %></div> Trace of template inclusion: app/views/statuses/new.html.erb
indeed, after running the application,
Thierry

Richard Wigley
3,733 PointsOK. 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:
rails console
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?
Rich

Thierry MELLERAUD DE VILLARS
414 PointsIt looks like the same, i confirm

Richard Wigley
3,733 PointsBy 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:
rake db:migrate:status
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
Rich

Thierry MELLERAUD DE VILLARS
414 PointsHello Rich,
Well i get the following result :
Status Migration ID Migration Name
up 20130609171415 Create statuses up 20130617191642 Devise create users up 20130617200718 Add user id to statuses
And unfortunately, same error when i run "post a new status"

Richard Wigley
3,733 PointsHello Thierry
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).
Rich
Reference Rails Guide on Migrations

Thierry MELLERAUD DE VILLARS
414 PointsNo change to report in devise_create_users migration file :
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
t.string :first_name
t.string :last_name
t.string :profile_name
## Database authenticatable
t.string :email, :null => false, :default => ""
t.string :encrypted_password, :null => false, :default => ""
So for now, i look the same information
Thank you again for your help and your patience

Richard Wigley
3,733 PointsLooks 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 ;-)
Rich

Thierry MELLERAUD DE VILLARS
414 PointsYou're right, the problem is solved and explained in the video that comes after the stage of migration. I pursue the course and thank you again for your help. It's nice to have such precise answers and so little time.
I love Teamtreehouse ! ;-))

Richard Wigley
3,733 PointsGreat news, good luck * bows out * :-)
Rich
Thierry MELLERAUD DE VILLARS
414 PointsThierry MELLERAUD DE VILLARS
414 PointsDespite the creation of the database from the beginning, migration and what followed, it always gives the same error :-( :
boldNoMethodError in Statuses#new
undefined method `name' for #<Status:0x007ff9ee8bda18>
Extracted source (around line #16):
Trace of template inclusion: app/views/statuses/new.html.erb
I think you've tried everything :-)