I have two controllers one is Payments and one is Transactions, I need to call the create method of transactions inside the create method of payments. So that each time I create a payment a transaction is automatically created. How should I approach this?
module Api class PaymentsController < ApplicationController before_action :set_payment, only: %i[ show update destroy ] def index @payments = Payment.all render json: @payments end def show render json: @payment end def create @payment = Payment.new(payment_params) if @payment.save render json: 'Payment Is Made sucessfully'.to_json, status: :ok else render json: @payment.errors, status: :unprocessable_entity end end private def payment_params params.permit(:currency, :amount, :payment_type, :payment_address) end end end
module Api class TransactionsController < ApplicationController before_action :set_transaction, only: %i[show update destroy] def index @transactions = Transaction.all render json: @transactions end def show render json: @transaction end # POST /transactions def create @transaction = Transaction.new(transaction_params) if @transaction.save render json: @transaction, status: :created, location: @transaction else render json: @transaction.errors,status::unprocessable_entity end end private def transaction_params params.permit(:transaction_type, :bank_name, :debit, :credit, :total_amount) end end end