Search code examples
ruby-on-railsdeviserails-postgresql

How do I automatically generate a new row in a table when the user signs up?


I'm trying to edit the devise sign up flow so that when a new user signs up I automatically add a row to a specific table with all default values. What is the best way to do this?

Some thought process:

  1. Edit the devise registrations_controller - My concern is I will then have to do more editing above and beyond what I am trying to accomplish.
  2. Force a check after the user signs up to see if the row is already there and since they just signed up it won't be and I can add it then... this seems hacky though...

Thank you!


Solution

  • You can add an after_create callback to your User model.

    class User < ApplicationRecord
      after_create :set_defaults
    
      def set_defaults
        SomeTable.create(user_id: self.id, column1: some_value, column2: some_value.....)
      end
    end