samedi 18 mai 2019

How to pass the params to two different tables in rails

As a newbie I started to do API POC. I have a situation as explained below:

I have seekerController which has create method.I want that when a Post request makes then few parameters has to go seeker table and few needs to go profile table(This table also have the seekerID column). I want to do this with in Transaction commit. So after reading I started doing below:-

ActiveRecord::Base.transaction do
          seeker = Seeker.new(seeker_params)
          seeker.save!
          params[:seeker_id] = seeker[:id]

          seekerprofile = SeekerProfile.new(seekerprofile_params)
          seekerprofile.save!
          end
      render json: {status: 'success', message: 'Request is processed successully', data:seeker},status: :created;

I have below definition:(I have doubt i the below way is correct)

def seeker_params
    params.require(:seeker).permit(:username, :alias, :mobile_number, :country_code, :email_address, :description, :status)
  end
  def seekerprofile_params
    params.require(:seeker_profile).permit(:seeker_id, :first_name, :middle_name, :last_name, :date_of_birth, :pincode, :building_name, :address, :email_address, :description, :status)

  end

its not working. I am not finding way to do it.also how to prepare seeker_params and seekerprofile_params.

Aucun commentaire:

Enregistrer un commentaire