Ruby Migration Error: undefined method `id' for nil:NilClass - ruby-on-rails

Using Rails 4.2.6, Ruby 2.0.0, OSX 10.11
I'm trying to run the rails server. Before that, I run "rake db:migrate", then I get the following error:
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
undefined method `id' for nil:NilClass
I'm quite new on Ruby. Can someone explain the error and how to solve it?
Thank you!

I just ran into this as well. The answer above wasn't very helpful in this case because so much of running the migration is meta programed in Active Record.
For me, the issue was that I had written a migration like this:
create_table :user_table do |t|
t.id :reference_table_id
end
I fixed it by changing the middle line to t.integer instead because id is not a column type.
Without seeing your migration I cannot be sure this will solve your issue, but it's what worked for me!

Look at the stack trace of the error (the stuff on the terminal after the error is displayed)
You should see a pointer to the line of code where the error is. It'll probably be something like x.id where x is the nil value

Related

Migrations canceled: undefined method `int'

When I run rake db:migrate, show the following error:
-- create_table(:posts)<br>
rake aborted!
StandardError: An error has occurred, all later migrations canceled:`<br>
undefined method 'int' for #
<ActiveRecord::ConnectionAdapters::MySQL::TableDefinition:0x0055c088b9f520>
Did you mean? in?
Any ideas or suggestions?
I just change the migrate file in db/migrate from int to integer. Like this: t.integer: age
A reason this could happen is if you accidentally got a column name and a data type around the wrong way during rails generate model ....
For example, if you accidentally ran rails g model calculations references:appointment (instead of rails g model calculations appointment:references), then you'd have:
t.appointment :references
but it should be
t.references :appointment
The same mistake could happen with not just references, but any other data type too (e.g. integer, string and so on).

ruby on rails saving record create problems

I am facing the below error when saving the record into database after I added the new column to migration that is "fathername"
NoMethodError in StudentController#admission1
undefined method `fathername=' for #<User:0x7ffcd42a3a40>
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/attribute_methods.rb:255:in `method_missing'
/root/ansipro342/app/models/student.rb:141:in `create_user_and_validate'
/root/ansipro342/app/controllers/student_controller.rb:62:in `admission1'
Request
Parameters:
{"student"=>{"first_name"=>"Salman",
"fatheroccupation"=>"Lecturer",
"address_line1"=>"",
"address_line2"=>"",
"country_id"=>"141",
"roll_num"=>"100",
"is_email_enabled"=>"1",
"gender"=>"m",
"fathernic"=>"",
"biometric_id"=>"",
"admission_date"=>"2015-12-11",
"date_of_birth"=>"2010-12-11",
"fathername"=>"Kamran",
"nationality_id"=>"141"}
even the fathername column already exists but its showing the error pls help, I dont know what is wrong?
I think you just have to run the migrations rake db:migrate ! This is typically happening when you try to set an attribute value and this attribute column doesn't exists in the database.
Hope it helps :)

Hartl Tutorial: Can't Figure Out How To Fix This 1 Error

I am finishing the book and I've ignored this error message for too long. Please help me understand how to fix this. Thank you!
1) Error:
PasswordResetsTest#test_password_resets:
NoMethodError: undefined method reset_sent_at=' for #<User:0x007f814e118600>
app/models/user.rb:63:increate_reset_digest'
app/controllers/password_resets_controller.rb:12:in create'
test/integration/password_resets_test.rb:17:inblock in '
The error might point you a little bit into the wrong direction. This NoMethodError is actually caused by that fact that your users table doesn't have a column reset_sent_at.
Rails defines accessor methods (getters and setters) on your User model for each column in the users table. However, since it doesn't have a reset_sent_at column, no accessor is defined and the NoMethodError is being raised.
Make sure you've created the migration where reset_sent_at was added to users (this was done in chapter 10 of the Rails tutorial), and that you have executed the migration as well. After the migration, be sure to restart your Rails server.

rails 2.3.5 - bug makes ActiveRecord::Base.configurations false. How do I track it down?

I've been updating my user test server and now suddenly I got an error every time I invoke rake with anything database-related.
Sample error:
rake db:drop RAILS_ENV='production' --trace
rake aborted!
undefined method `[]' for false:FalseClass
/usr/local/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/tasks/databases.rake:96
Here's line 96 of that databases.rake:
config = ActiveRecord::Base.configurations[RAILS_ENV || 'development']
So it seems that Base.configurations is ´false´ instead of being an array.
I've been fighting this for a couple hours now but I could not find anything on my source.
Google didn't provide any useful hints.
Could anyone point out any obvious reasons why this member is false?
I'm going to accept my own question since Karl seems to be unavailable, and this lowers my acceptance rate.
The problem was that my database.yml was missing.
Karl, if you answer this question, I'll give the answer to you.

Couldn't create database, undefined method `quote_ident'?

rake db:create
I get the following error:
undefined method `quote_ident' for PGconn:Class
Having googled for a solution, it seems by adding:
def PGconn.quote_ident(name)
%("#{name}")
end
to config/initializers/new_rails_defaults.rb should solve the problem, but I'm still getting the error! Any suggestions? Thank you
See error here: http://github.com/mneumann/postgres-pr/issues/issue/1
The solution is to add:
def PGconn.quote_ident(name)
%("#{name}")
end
to active_record/connection_adapters/postgresql_adapter.rb
I would think this is a version mismatch.
Are you sure you have the right versions of everything installed? E.g. Postgres server, libpgsql (Postgres client library), whatever library Ruby has for accessing Postgres, and finally your application (rake)?

Resources