在 rails 5 api 应用程序中处理嵌套属性

handling nested attributes in rails 5 api only application

我在 rails api 申请中有一个要求。客户可以有很多订单,每个订单属于一个客户。

class Client < ApplicationRecord
    has_many :orders
end

我的 order.rb 是

class Order < ApplicationRecord
  belongs_to :client, dependent: :destroy
  accepts_nested_attributes_for :client
  validates_presence_of :order_amount, :service_amount, :miners_amount
end

我公开了一条路由 /place_order 并创建了客户端和订单。

class OrderProcessingController < ApplicationController
    def place_order
        @order = Order.new(order_processing_params)
        if @order.save
            render json: @order
        else
            render json: @order.errors.full_messages
        end
    end

    private
    def order_processing_params
        params.require(:order).permit(:order_amount, :service_amount, :miners_amount, client_attributes: [:name, :email, :phone_number, :date])
    end
end

到目前为止一切正常。现在我的要求是,我必须检查客户端是否已经存在于客户端 table 中。如果是,请为订单添加 client_id 并创建新订单。我不想每次都创建新客户和订单。

我怎样才能在 before_filter 或类似的东西中达到同样的效果。从客户端参数获取客户端,如果客户端存在,则从传入参数中删除参数键 ???

place_order的post数据如下

{
     "order" : {
       "order_amount" : "10000",
        "service_amount" : "1000",
        "miners_amount" : "10000",
         "client_attributes": {
                "name": "Ajith",
                "email": "ajith@gmail.com",
                "phone_number": "12231321312",
                "date": "12/12/12"
            }
         }
  }

提前致谢, 阿吉特

以下代码未经过测试,大多数情况下您的方法应该围绕此

class OrderProcessingController < ApplicationController
  before_action :find_client, only: [:place_order]

  def place_order
    @order = @client.orders.new(order_processing_params)

    if @order.save
        render json: @order
    else
        render json: @order.errors.full_messages
    end
  end

private
  def order_processing_params
    params.require(:order).permit(:order_amount, :service_amount, :miners_amount, client_attributes: [:name, :email, :phone_number, :date])
  end

  def find_client
    @client = Client.find_or_create_by(email: params[:order][:client_attributes][:email])
    #below line can be improved using a method, see the last line if later you want, never update a primary key which is email in bulk params
    @client.update_attributes(name: params[:order][:client_attributes][:name], phone_number: params[:order][:client_attributes][:phone_number], date: params[:order][:client_attributes][:date])
  end

  #def get_client_params
   # params.require(:order)
  #end
end

我尝试了以下方法来获得解决方案。不太确定这是解决问题的正确方法

class OrderProcessingController < ApplicationController
    before_action :find_client, only: :place_order
    def place_order
        if @client.present?
            @order = @client.orders.build(order_processing_params) 
        else
            @order = Order.new(order_processing_params)

        end
        if @order.save
            render json: @order
        else
            render json: @order.errors.full_messages
        end
    end

    private
    def order_processing_params
        params.require(:order).permit(:order_amount, :service_amount, :miners_amount, client_attributes: [:name, :email, :phone_number, :date])
    end

    def find_client
        begin
            @client =  Client.find_by_email(params[:order][:client_attributes][:email])
        rescue
            nil
        end
    end
end

谢谢, 阿吉特