[英]Create new record based upon the presence of a record with Rails
I've got a rails app that has two models: address
and delivery
, where address has_many:deliveries
and delivery belongs_to:address
.我有一个 Rails 应用程序,它有两个模型:
address
和delivery
,其中address has_many:deliveries
和delivery belongs_to:address
。
I'm attempting to create a means by which a single POST request can search to find a matching address
in the addresses table using parameters provided in the request, if a match is found create a new delivery
.我正在尝试创建一种方法,如果找到匹配项,则单个 POST 请求可以使用请求中提供的参数在地址表中搜索以找到匹配的
address
,如果找到匹配项,则创建一个新的delivery
。 If no address is found, then error the request.如果没有找到地址,则错误请求。
Here's what I have so far (not functional, but hoping I'm close(ish)):这是我到目前为止所拥有的(没有功能,但希望我很接近(ish)):
Controller Controller
class DeliveriesController < ApplicationController
before_action :confirm_address, only: %i[ create edit update ]
protect_from_forgery with: :null_session, if: Proc.new {|c| c.request.format.json? }
def index
render json: Delivery.all
end
def create
# Delivery.address_id = Address.find_by(numeric: params[:numeric], street_name: params[:street])
# @address_of_delivery = Delivery.includes(:address).where(numeric: params[:numeric], street_name: params[:street_name], city: params[:city], state: params[:state], zip: params[:zip])
delivery = Delivery.new(delivery_params)
# @confirmed_address.id = self.address_id
puts 'putting'
# puts @address_of_delivery
if delivery.save
render json: {status: 'Awesome!', data: delivery}, status: :ok
else
render json: {status: 'Boo!', data: delivery.errors}, status: :unprocessable_entity
end
end
private
def delivery_params
params.permit(:company, :numeric, :street_name, :city, :state, :zip)
end
def confirm_address
@confirmed_address = Address.find_by(numeric: params[:numeric], street_name: params[:street_name], city: params[:city], state: params[:state], zip: params[:zip])
if @confirmed_address.present?
puts "found one!"
else
puts "NO DICE"
# self.address_id = confirmed_address.id
end
end
As you can see from the controller, I've attempted several paths including using includes
and join
(not included in snippet), as well as trying to just hard code it.正如您从 controller 中看到的那样,我尝试了几种路径,包括使用
includes
和join
(未包含在代码段中),以及尝试对其进行硬编码。
Also tried to solve through the model, but had trouble accessing the params from the POST request:还尝试通过 model 解决,但无法从 POST 请求访问参数:
class Delivery < ApplicationRecord
belongs_to :address
# accepts_nested_attributes_for :address
attribute :numeric
attribute :street_name
attribute :city
attribute :state
attribute :zip
before_validation :find_associated_address
def find_associated_address
Address.find_by(numeric: params[:numeric], street_name: params[:street])
puts 'working in model'
end
end
Copy of the POST request for clarity:为了清楚起见,POST 请求的副本:
Started POST "/deliveries" for ::1 at 2021-11-20 22:52:00 -0600
Processing by DeliveriesController#create as */*
Parameters: {"company"=>"slowcart", "numeric"=>"123", "street_name"=>"Any Street", "city"=>"NYC", "state"=>"NY", "zip"=>"010101"}
I'm almost certainly making a stupid mistake, but can't find the right means to do what I want to do in the docs/broader web.我几乎肯定犯了一个愚蠢的错误,但是在文档/更广泛的 web 中找不到正确的方法来做我想做的事情。 Rails 6 application, btw.
Rails 6 应用程序,顺便说一句。
If you are just looking for a match in addresses table, why not just do:如果您只是在地址表中寻找匹配项,为什么不这样做:
Address.where(numeric: params[:numeric], street_name: params[:street_name], city: params[:city], state: params[:state], zip: params[:zip])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.