0

I have a devise model that differs from the default. It has extra attributes.

For example, my devise user has an email, password, and also a career. I'd like to write a line of code that will delete the career from the currently signed in user. I like to imagine that it would look something like this

current_user.career.delete

This would ideally empty the career from the database, so that current_user.career would then return nil until the user is updated again to add a career.

How can I do this?

johncorser
  • 9,262
  • 17
  • 57
  • 102

1 Answers1

1

The delete method take the id or array of ids as parameter so you can simply use

current_user.career = nil
current_user.save

or you can set career to nil by create migration, see this question

change_column :users, :career, :string, :default => nil

see the documentation to learn more.

Community
  • 1
  • 1
Alaa Othman
  • 1,109
  • 12
  • 16
  • Actually, this does not work because it only changes the value of current_user.career, it does not actually delete the information from the database. – johncorser Oct 06 '14 at 21:06
  • I also don't think I can use change_column because I want to do this as part of a larger script rather than in a separate migration. – johncorser Oct 06 '14 at 21:09