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 :)
Related
After upgrading to rails 5.2 and running rake db:migrate I get the error below
ActiveRecord::NotNullViolation: Mysql2::Error: Field 'key' doesn't have a default value: INSERT INTO 'ar_internal_metadata'
I understand this is a new thing in rails 5 and above to prevent potential data loss in production. I can not find anywhere in the schema or migrations where this table is generated. It creates the key column with no default value and it can not be changed since it's a primary key. Any help would be greatly appreciated.
After some more research, I discovered that the values in the auto-generated ar_internal_metadata table only get set if you have an active migration while running rake db:migrate. The workaround is to set the values manually in the table so:
key -> environment and value -> production
hope this helps someone else.
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
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.
I'm relatively new to Rails and I'm running into an issue while trying to set up Devise. I believe the issue stems from the fact that I already generated a user scaffold before trying to install Devise, yet I don't know how to solve this problem. When I proceed through the Devise setup, I get to the step where I have to enter the following code:
rails generate devise User
That works, and I get this back from terminal:
invoke active_record
create db/migrate/20120609032448_add_devise_to_users.rb
insert app/models/user.rb
route devise_for :users
The next step is to migrate the database, which I attempt but get the following error:
== AddDeviseToUsers: migrating ===============================================
-- change_table(:users)
rake aborted!
An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: duplicate column name: email: ALTER TABLE "users" ADD "email" varchar(255) DEFAULT '' NOT NULL
Tasks: TOP => db:migrate
I've tried destroying the original User scaffold along with the original User migration but I keep getting stuck at this error. Help would be much appreciated!
Your new migration probably has an email column defined in it. Comment that line regarding adding an email column out and run it again. It's likely you've already got an email column in your model.
Try going to this file
db/migrate/20120609032448_add_devise_to_users.rb
and in the code where it says
change_table(:users)...
alter this to
create_table(:users)...
HI,
I got this error when i am testing my rails application. I dont have the table named 'instructions'. But it shows a error like "ERROR: relation "instructions" does not exist". Totally, I got same error for 64 tests as 64 errors.
I am using rails 3.0, Ruby 1.9.2, Netbeans 6.8.
PS: I didnt creat Instruction manual for rails application.
Error:
test_should_get_index(HomeControllerTest):
ActiveRecord::StatementInvalid: PGError: ERROR: relation "instructions" does not exist
LINE 1: DELETE FROM "instructions"
^
: DELETE FROM "instructions"
Kindly help me in this regard
You may want to run rake db:test:prepare , the table instructions isn't present in your test database.
Check your fixtures. I hit an error like this when using Rails scaffolding to generate a subclassed resource.
The generator had created files in my test/fixtures/ directory that didn't correspond to any actual tables in my database (since the model was using STI under another table).
When running tests, Rails attempted to instantiate all my fixtures, and an error similar to yours was generated when it could not find a table matching the name of the fixture.
Deleting the unneeded fixture file cleared my error and got tests running.
Hope that helps you, or others who arrive here searching that error, as I did. :]