Ruby on rails HABTM与rails API的关系不起作用
我是RubyonRails新手。我正在使用Rails 5.1、活动记录序列化程序、doorkeeper和Desive gem制作Rails API 我有一张Ruby on rails HABTM与rails API的关系不起作用,ruby-on-rails,json,ruby,api,has-and-belongs-to-many,Ruby On Rails,Json,Ruby,Api,Has And Belongs To Many,我是RubyonRails新手。我正在使用Rails 5.1、活动记录序列化程序、doorkeeper和Desive gem制作Rails API 我有一张订单表格,里面有很多产品。订单和产品之间的关系是多对多的 订单型号: class Order < ApplicationRecord validates_presence_of :brute, :net has_and_belongs_to_many :products end 产品序列化程序: cl
订单
表格,里面有很多产品。订单和产品之间的关系是多对多的
订单型号:
class Order < ApplicationRecord
validates_presence_of :brute, :net
has_and_belongs_to_many :products
end
产品序列化程序:
class ProductSerializer < ActiveModel::Serializer
attributes :id, :name, :price, :description
has_one :category
end
我试图找到解决方案,但失败了。据我所知,Rails在给定ID列表时不会自动处理创建连接记录的问题。因此,当您调用
@order=order.new(order\u params)
并希望它知道如何处理产品ID:[3]
时,它只是忽略了它
如果使用下面的命令修改create
端点,您应该会看到正在创建的连接记录
# 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
最后我解决了你的问题。只需在模型中添加一个属性 订单型号:
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, :products
def products
object.products.map do |product|
ProductSerializer.new(product, scope: scope, root: false, event: object)
end
end
end
class OrderSerializer < ActiveModel::Serializer
attributes :id, :discount, :brute, :net, :payed, :payed_at
has_many :products
end
类OrderSerializer# 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
#POST/api/orders
def创建
@订单=订单。新建(订单参数)
如果@order.save
#查找产品
@产品=产品。其中(id:order\u params[:Product\u id])
#创建联接表记录
@products.each{| product | product.orders我不使用序列化程序,因此我对您的一些代码不太熟悉。尽管您能指出您在代码中向订单添加产品的位置吗?也许您可以复制视图(erb)处理此问题的文件。我也面临同样的问题。但在使用活动记录序列化程序gem.order_params[:product_id]时,没有erb文件当我从json API请求时返回nil。为什么?听起来您有两个问题。第一个问题是没有创建联接表记录。我上面介绍的解决方案应该可以解决这个问题,您是否验证了现在正在创建记录?如果是,并且json输出仍然不正确,那么您的订单序列化程序是错误的。添加上面的信息。您给定的方法是正确的,我检查了@products=Product.where(id:3),然后创建了联接表。但是当我调试并找到order_参数[:Product_id]返回始终为零且order_params不包含任何产品ID!我的json格式不正确吗?您的可序列化_哈希
返回错误,它需要两个缺少的参数。更新了代码,删除了传递到ProductSerializer
中的多余参数。我已在本地存储库上对此进行了测试,它可以作为expec泰德。如果你仍然有错误,你需要发布更多细节,甚至可能链接回购协议。发布你收到的全部错误。没有详细信息,没有人能帮你解决这个问题。谢谢@Engr。坦比尔·哈桑,你应该得到一枚奖牌。
class OrderSerializer < ActiveModel::Serializer
attributes :id, :discount, :brute, :net, :payed, :payed_at, :products
def products
object.products.map do |product|
ProductSerializer.new(product).serializable_hash
end
end
end
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
# 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