Reputation: 1
I have created a basic bookkeeping application with an Accounts Model, this was a basic scaffold. I have implemented the devise gem to only display accounts that were created with the same user_id as the current and its all working perfectly. How would I now create a set of accounts that would be set as default accounts for new users when they sign up, instead of them creating all of the accounts themselves?
Upvotes: 0
Views: 62
Reputation: 102194
You can tap into the Devise controller by calling super with a block:
# config/routes.rb
devise_for :user, controllers: {
registrations: 'my_registrations'
}
# app/controllers/my_registrations_controller.rb
class MyRegistrationsController < Devise::RegistrationsController
def create
super do |user|
5.times { user.accounts.create }
end
end
end
Devise yields the block after the user has been saved. This approach is generally preferable to model callbacks since you have better control of exactly where in your application it is performed and it will not slow down tests.
The logic of seeding an account is also a prime candidate for extraction into a service object.
Upvotes: 1