Fernando Maymone
Fernando Maymone

Reputation: 355

Setting a basic entity while creating a new user in Devise

Im using Devise to create my users in an App with Ruby on Rails.

I have a User model that has a Plan (hobby,premium, etc...)

When creating a new user, I want to add the basic plan to this new user (for business rules needs, I cant leave it blank).

The question is, how can I add this plan when creating this new user?

Here is my controller:

class RegistrationsController < Devise::RegistrationsController
  clear_respond_to
  respond_to :json

  def save_user_type
    session[:user_type] = params[:user_type]
  end

  private

  def sign_up_params
    params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :type, :provider )
  end

end

In which method should I add something like this?

@user.plan = Plan.first
@user.save

Upvotes: 0

Views: 76

Answers (2)

Fernando Maymone
Fernando Maymone

Reputation: 355

Added this line to the user model

after_create do |user|
  user.plan = Plan.first
  user.save
end

Upvotes: 0

Anand
Anand

Reputation: 6531

class User < ActiveRecord::Base        
  has_one :plan

  after_create :build_default_plan

  private
  def build_default_plan
    plan.create(#paln_params)
    #.. so on
  end
end

Upvotes: 1

Related Questions