【问题标题】:Rails - Adding to an array in dbRails - 添加到数据库中的数组
【发布时间】:2015-07-23 17:17:51
【问题描述】:

我在一个名为“当前计划”的表中有一个名为“计划选择”的列,我希望能够向这些数组添加其他值。

例如:如果数组当前是 ["a", "b", "c"],我希望能够将第四个元素 "d" 添加到数组中。

到目前为止,我已经在 Current Plans 控制器中创建了一个新操作:

def addplan
    p = CurrentPlan.find(12)
    p.plan_selection << "Test"
    p.save
    redirect_to:back
end 

然而,虽然代码没有中断,但它除了将我重定向回同一页面外,什么也没做。

Started POST "/clients/1/quotes/36/current_plans/12/addplan" for 66.186.164.130 at 2015-07-23 17:03:43 +0000
Processing by CurrentPlansController#addplan as HTML
  Parameters: {"authenticity_token"=>"83OuJJLivjOCXgzoKkPnzqz8HBk=", "client_id"=>"1", "quote_id"=>"36", "current_plan_id"=>"12"}
  CurrentPlan Load (30.6ms)  SELECT  "current_plans".* FROM "current_plans"  WHERE "current_plans"."id" = $1 LIMIT 1  [["id", 12]]
   (30.5ms)  BEGIN
   (30.3ms)  COMMIT
Redirected to http://crossing-mmiller.c9.io/clients/1/quotes/36/current_plans/12/
Completed 302 Found in 240ms (ActiveRecord: 213.1ms)

路线:

resources :current_plans do
    collection { post :planselection }
    post :addplan
end

任何帮助将不胜感激!

当前计划架构:

create_table "current_plans", force: true do |t|
    t.integer  "client_id"
    t.string   "client_name"
    t.string   "plan_name"
    t.integer  "plan_year"
    t.string   "classification"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "plan_number"
    t.date     "effective_date"
    t.string   "quote_name"
    t.string   "plan_selection", default: [], array: true
    t.integer  "quote_id"
    t.integer  "benefit_detail_id"
    t.float    "group_premium"
end

当前计划模型:

class CurrentPlan < ActiveRecord::Base
   belongs_to :quote
   belongs_to :client
end

【问题讨论】:

  • 显示您的 CurrentPlan 模型和 current_plans 表的架构将有助于解决问题。
  • 在模型中定义serialize :plan_selection, Array
  • 尝试将p.save 替换为p.save!。如果保存不成功,这将引发错误,并且可能有助于找到问题。
  • boulder - 我添加了您要求的信息。 @Sontya - 我添加了你的建议,我现在有一个新的动作控制器错误 :PG::InvalidTextRepresentation: ERROR: array value must start with "{" or dimension information. 终端输出: SQL (31.0ms) UPDATE "current_plans" SET "plan_selection" = $1, "updated_at" = $2 WHERE "current_plans"."id" = 12 [["plan_selection", "-- -\n- Gold PPO 1500 90/70\n- Gold PPO 1000 90/70\n- Gold AWH HNOnly 1500 90/60\n- Test\n"], ["updated_at", "2015-07-23 18 :31:41.197886"]]
  • btw...如何格式化内联代码?

标签: ruby-on-rails arrays activerecord


【解决方案1】:

问题是您使用的语句没有将字段标记为脏,因此记录没有更新。这是 Rails 中序列化字段的一个已知问题。 解决方案是使用不同的语法:

p.plan_selection = p.plan_selection.push("测试")

顺便说一句,表字段是一个数组,Rails 通过读取模式已经知道了,因此您不需要也不应该在模型中将其标记为序列化。

【讨论】:

  • 仍然没有将“测试”保存到数组中。没有错误,但也没有变化。感谢您的帮助!
  • 我发布了另一个可见性答案,但您的解释是关于为什么它不起作用,但是,我不得不采取不同的方法来解决这个问题。感谢您的帮助。我会投票给你,但我没有足够的声誉。
  • 没问题;很高兴能帮助你。你是对的,p.plan_selection_will_change! 是这样做的方式。
【解决方案2】:

@boulder 是正确的,因为该字段没有被标记为脏,但结果证明有效的解决方案是在将“测试”推入数组之前添加此行 p.plan_selection_will_change!

【讨论】:

  • 这个页面有很多关于 Rails 4 和 Postgres 主题的见解:link
猜你喜欢
  • 1970-01-01
  • 2017-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多