gpt4 book ai didi

arrays - 如何在 Active Admin Rails 4 中保存数组?

转载 作者:行者123 更新时间:2023-12-04 01:59:29 25 4
gpt4 key购买 nike

我在 Rails 4 中使用 Active Admin。在我的模型中,当我创建它在后台传递的对象时,我有一个字段是 Postgres 数组类型,但它没有保存到数据库中。那么我需要做什么才能通过 Active Admin 将数组字段保存在数据库中。

谢谢

最佳答案

你有破解的东西,因为事件管理员似乎不支持它。

虽然这个答案很旧,但它有效:How do you handle serialized edit fields in an Active Admin resource?

Here is a summary of how I handled this situation. I added an accessor to the model which can turn the Array into a string joined by a linefeed and split it back to an Array.


# app/models/domain.rb
class Domain < ActiveRecord::Base
serialize :names, Array
attr_accessor :names_raw

def names_raw
self.names.join("\n") unless self.names.nil?
end

def names_raw=(values)
self.names = []
self.names=values.split("\n")
end
end

then, in my admin resource for domain, instead of using the :names field, I used the :names_raw field. setting this value would save the names Array with the new values.


# app/admin/domains.rb
form do |f|
f.inputs "Domain" do
f.input :names_raw, :as => :text
end
f.actions
end

关于arrays - 如何在 Active Admin Rails 4 中保存数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19519629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com