Reputation: 10551
Not sure why this is happening:
2.0.0p247 :001 > User.column_names
=> ["id", "user_name", "email", "password_digest", "created_at", "updated_at", "register_key", "culminated", "remember_token", "register_token_created_at", "profile_image", "licence_image", "first_name", "last_name", "nearest_town"]
2.0.0p247 :002 > user1 = User.create(user_name: 'james', email: '[email protected]', first_name:'jj', last_name:'jj', nearest_town:'mordor')
=> #<User id: nil, user_name: "james", email: "[email protected]", password_digest: nil, created_at: nil, updated_at: nil, register_key: nil, culminated: nil, remember_token: nil, register_token_created_at: nil, profile_image: nil, licence_image: nil, first_name: "jj", last_name: "jj", nearest_town: "mordor">
2.0.0p247 :003 > user1.update(user_name: 'killo')
=> false
Rather than a solution, how would you go about debugging this problem from the console?
Upvotes: 1
Views: 86
Reputation: 53048
If you notice:
2.0.0p247 :002 > user1 = User.create(user_name: 'james', email: '[email protected]', first_name:'jj', last_name:'jj', nearest_town:'mordor')
=> #<User id: nil, user_name: "james", email: "[email protected]", password_digest: nil, created_at: nil, updated_at: nil, register_key: nil, culminated: nil, remember_token: nil, register_token_created_at: nil, profile_image: nil, licence_image: nil, first_name: "jj", last_name: "jj", nearest_town: "mordor">
User record (user1) was not created at all. User id is nil. You must be having some failed validations. If the record would have been successfully created in the database then your user id
would never be nil
as its the primary key.
Try with User.create! instead so you know that why the record was not created, you will get the exact exception raised. For example:
2.0.0p247 :002 > user1 = User.create!(user_name: 'james', email: '[email protected]', first_name:'jj', last_name:'jj', nearest_town:'mordor')
Upvotes: 2
Reputation: 6263
Seems like your object is not valid. As Kirti pointed out it has not been persisted to the database as no primary key id has been returned. Checking the validity of your object would give you more information on what is up with your object. Checkout rails guides for a breakdown of validation.
Upvotes: 0
Reputation: 51191
Your User
record is not saved, probably because of failed validation.
You should check its validity with:
user1.valid?
and show errors:
user1.errors.full_messages
Upvotes: 3