简体   繁体   English

Rails,将数组提交给postgres

[英]Rails, submitting array to postgres

I have a Rails API and I am trying to pass an array of numbers to my postgres db but I can't: I send an array with two values, and it accepts an array with only the first value.我有一个 Rails API,我试图将一个数字数组传递给我的 postgres 数据库,但我不能:我发送一个包含两个值的数组,它接受一个只有第一个值的数组。

I have 2 models, Itinary and Event where:我有 2 个模型, ItinaryEvent ,其中:

class Event 
  belongs_to: itinary
  accepts_nested_attributes_for :itinary

class Itinary
   has_many: events

I use Postgres and a React front end and Rails API with Postgres.我使用 Postgres 和一个 React 前端和 Rails API 和 Postgres。 I declared a column of type array with values if type decimal :如果类型为decimal ,我声明了一列类型为array的列:

  • Rails Postgres schema: I set DECIMAL as the type: Rails Postgres 架构:我将DECIMAL设置为类型:
create_table "itinaries", force: :cascade do |t|
    ...
    t.decimal "start_gps", default: [], array: true
  • Rails EventsController : nothing fancy, from the Event controller, the create method is: Event.new(event_params) , and the strong params method is also rather simple: Rails EventsController :没什么花哨的,从Event controller,create方法是: Event.new(event_params) ,strong params方法也比较简单:
def event_params
   params.require(:event).permit( 
      ...,
      itinary_attributes: [...,  start_gps: [] ],
      ...
   )
end

I checked that Event.first.itinary.start_gps.class returns Array .我检查了Event.first.itinary.start_gps.class返回Array I can seed with Itinary.create.(..,: start_gps,[45,1]) (the values are numbers).我可以使用Itinary.create.(..,: start_gps,[45,1]) (值是数字)。

  • React form: the data are sent within a FormData from React where I believe I used the correct Rails naming convention: React 表单:数据是在 React 的FormData中发送的,我相信我使用了正确的 Rails 命名约定:
const fdata = new FormData()
fdata.append("events[itinary_attributes][start]", itinary.start)
fdata.append("events[itinary_attributes][start_gps][]", itinary.start_gps)
...

so the header of my POST request contains:所以我的 POST 请求的 header 包含:

event[itinary_attributes][start_gps][]: 45.1936513315257,0.6646728515625

On submit, I get almost the desired params but the values in the array are quoted:提交时,我得到了几乎所需的参数,但引用了数组中的值:

 Parameters: 
{"event"=>{
   "itinary_attributes"=>{
       "date"=>"2020-08-16",
       "start"=>"Paris FRA",
       "start_gps"=>["48.85011347181819,2.3353889957070355"], << one value ?
       }
   }
}

so that I get only the first value when Postgres fires:这样我在 Postgres 触发时只得到第一个值:

 Itinary Create (2.5ms)  
INSERT INTO "itinaries" ...
  ["start_gps", "{48.85011347181819}"], <<<<< ONLY FIRST
                   ^^
]
  • If I set t.string or t.text as the data-type in the schema, Postgres fires:如果我将t.stringt.text设置为模式中的数据类型,Postgres 会触发:
Itinary Create (2.5ms) 
 ["start_gps", "{\"45.18978009667531,0.6811523437500001\"}"]]

so Event.last.itinary.start_gps.length returns 1, a concatenated value:所以Event.last.itinary.start_gps.length返回 1,一个连接的值:

start_gps: Array(1)
=> "45.158800738352106,1.5051269531250002"

I even tried to submit an array of strings from React without success.我什至尝试从 React 提交一个字符串数组,但没有成功。 Where to start?从哪儿开始? This is something very classical I believe.我相信这是非常经典的东西。 Looks like Rails or Postgres can't read the comma ',' that separates two values in an array.看起来 Rails 或 Postgres 无法读取分隔数组中两个值的逗号 ','。

You might want to try this:你可能想试试这个:

const fdata = new FormData()
fdata.append("events[itinary_attributes][start]", itinary.start)
fdata.append("events[itinary_attributes][start_gps][]", itinary.start_gps[0])
fdata.append("events[itinary_attributes][start_gps][]", itinary.start_gps[1])

This should get you这应该让你

{"event"=>{
  "itinary_attributes"=>{
    "date"=>"2020-08-16",
    "start"=>"Paris FRA",
    "start_gps"=>["48.85011347181819", "2.3353889957070355"] <-- Two values in an array
   }

In case of any interest, one can solve this 'manually': to save an array of numbers into an array field, I split the received params.如果有任何兴趣,可以“手动”解决这个问题:为了将一组数字保存到一个数组字段中,我拆分了接收到的参数。 This is unsatisfactory as such a simple job demands so much gesticulation.这是不能令人满意的,因为如此简单的工作需要如此多的手势。 I still don't see what's wrong.我仍然不明白有什么问题。

One solution is to add in the controller:一种解决方案是添加 controller:

if params[:event][:itinary_attributes][:start_gps]
      params[:event][:itinary_attributes][:start_gps] = params[:event][:itinary_attributes][:start_gps][0].split(',')
end

event_params = params.require(:event).permit( 
   ...,
   itinary_attributes: [...,:start, start_gps: [], end_gps: []],  
   ) 
Event.new(event_params)
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM