GDMN
GDMN

Reputation: 184

Creating a User must create a new Record with the id Parameter of the User

I have two models ; User and Record. I would like to create automatically a new Record when creating a new User. The new Record must have an attribute which is the id of the newly created User.

I have read about using link_to, to pass a parameter but I deem this is not what I need. I'd like the code to be in the User Controller in the create action.

So I wrote this code in my controller :

  def create
    @user = User.new(user_params)

    @record = Record.new(:user_id=>@user.id)
    @record.save

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
      format.js
    end
  end

To check if it could work I wrote this instead of what is in the previous code. And it works I have a new record with an id for the user of 1

    @Record = Record.new(:user_id=>"1")
    @Record.save

How can I get instead of 1 the id of the User that is created ?

I tried several options :

params[:id]
User.find(params[:id])

Upvotes: 0

Views: 348

Answers (4)

Pramod Solanky
Pramod Solanky

Reputation: 1690

I don't think that the code in @GDMN's answer will work.

As soon as the user record is saved, it would either redirect if the request is of type html or it would render something in case of a json request. This line @record = Record.create(user: @user) would execute for request types other than html and json and never otherwise. You could rather use the below code copied from @Vishnu's answer

@user = User.new(user_params)
@user.build_record

respond_to do |format|
  if @user.save
    format.html { redirect_to @user, notice: 'User was successfully created.' }
    format.json { render :show, status: :created, location: @user }
  else
    format.html { render :new }
    format.json { render json: @user.errors, status: :unprocessable_entity }
  end
  format.js
end

Upvotes: 0

GDMN
GDMN

Reputation: 184

I eventually found a solution that works well. This is the code I use in my controller so that creating a User automatically create a Record with the id of the User.

I have to precise that I have for these two models the following relations:

In the user model : has_one :record

In the record model : belongs_to :user

  def create
    @user = User.new(user_params)


    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
        @record = Record.create(user: @user)
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

That is all it takes.

Upvotes: 0

Vishnu Atrai
Vishnu Atrai

Reputation: 2368

Try below code -

 @user = User.new(user_params)
 @user.build_record # if you have has_one association in user model
 @user.records.build # if you have `has_many` association in user model

 @user.save

So your controller code should be -

def create
    @user = User.new(user_params)
    @user.build_record

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
      format.js
    end
  end

Upvotes: 1

Tim Kretschmer
Tim Kretschmer

Reputation: 2280

class User
  has_many :records, autosave: true

  after_initialize {
    # this is automatically creating a new record element
    # and save its association, but only if its a new record (user)
    records.build if self.new_record?
  }
end

#in your controller
def create
   @user = User.new(user_params)
      respond_to do |format|
    if @user.save
      format.html { redirect_to @user, notice: 'User was successfully created.' }
      format.json { render :show, status: :created, location: @user }
    else
      format.html { render :new }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
    format.js
  end
end

Upvotes: 0

Related Questions