我是 Ruby on Rails 的新手。我正在使用 Rails 5.1、事件记录序列化程序、门卫和设计 gem 制作 Rails API。
我有一个Order
表,它有很多产品。订单和产品之间是多对多的关系。
订购型号:
class Order < ApplicationRecord
validates_presence_of :brute, :net
has_and_belongs_to_many :products
end
产品型号:
class Product < ApplicationRecord
belongs_to :category
validates_presence_of :name, :price
validates_uniqueness_of :name
has_and_belongs_to_many :orders
end
我有一个名为 orders_products
的连接表。
订单序列化器:
class OrderSerializer < ActiveModel::Serializer
attributes :id, :discount, :brute, :net, :payed, :payed_at, :products
def products
object.products.map do |product|
ProductSerializer.new(product, scope: scope, root: false, event: object)
end
end
end
产品序列化器:
class ProductSerializer < ActiveModel::Serializer
attributes :id, :name, :price, :description
has_one :category
end
订单 Controller :
module Api
class OrdersController < ApiController
before_action :set_order, only: [:show, :update, :destroy]
# GET /api/orders
def index
@orders = Order.all
render json: @orders
end
# GET /api/orders/1
def show
render json: @order
end
# POST /api/orders
def create
@order = Order.new(order_params)
if @order.save
render json: @order, status: :created, location: api_order_url(@order)
else
render json: @order.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /api/orders/1
def update
if @order.present?
if @order.update(order_params)
render json: @order
else
render json: @order.errors, status: :unprocessable_entity
end
end
end
# DELETE /api/orders/1
def destroy
@order.destroy if @order.present?
end
private
# Use callbacks to share common setup or constraints between actions.
def set_order
@order = Order.find(params[:id])
rescue ActiveRecord::RecordNotFound
Rails.logger.error{ 'Order record is not found' }
nil
end
# Only allow a trusted parameter "white list" through.
def order_params
params.require(:order).permit(:discount, :brute, :net, :payed, :payed_at, product_ids: [])
end
end
end
当我从 API 生成器应用程序(如 Postman/Insomnia)发布一些订单 json 数据时,订单被保存在订单表中,但没有数据保存在 orders_products
连接表中。
我的订单 json 请求(POST http://localhost:3000/api/orders):
{
"discount": 110,
"brute": 100,
"net": 200,
"payed": null,
"payed_at": null,
"product_ids": [3]
}
我试图找到解决方案,但我失败了。
最佳答案
最后我解决了你的问题。只需在你的模型中添加一个属性。
订购型号:
class Order < ApplicationRecord
attribute :product_ids
validates_presence_of :brute, :net
has_and_belongs_to_many :products
end
订单序列化器:
class OrderSerializer < ActiveModel::Serializer
attributes :id, :discount, :brute, :net, :payed, :payed_at
has_many :products
end
并在您的订单 api 中创建方法:
# POST /api/orders
def create
@order = Order.new(order_params)
if @order.save
# Find products
@products = Product.where(id: order_params[:product_ids])
# Create join table records
@products.each { |product| product.orders << @order }
render json: @order, status: :created, location: api_order_url(@order)
else
render json: @order.errors, status: :unprocessable_entity
end
end
我已经在本地测试过并且有效!快乐编程:)
关于ruby-on-rails - HABTM 与 rails API 的关系不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46653186/