0

I have the following problem, but first I will make some assumptions

  1. The example is just to explain my problem in an easy way
  2. The tables models are not related

I have two tables (models) Users an Emails

Users

  • id
  • name
  • email

Emails

  • id
  • account

So, the idea is every time I create a user, I want to create an instance of Email, where Emails.account = Users.email

I tried using callback

def after_create
   Email.create!(:account => user.email)
end

But it didn't work.

Is there another way to achieve this?

1 Answer 1

1

You are almost there, except you don't need to reference a 'user' variable in your after_create because you are in the User model.

Try the following:

class User < ActiveRecord::Base 
  after_create :create_email
  def create_email
     Email.create!(:account => email)
  end
end
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.