如何将布尔值存储在JSONB列中

发布于 2025-01-19 21:42:40 字数 1424 浏览 5 评论 0 原文

我从我的表单中获取一个复选框值,如下所示

<%= f.label 'Most Popular', class: "form-label" %>
                    <%= check_box_tag "price_template[preferences][pcb_budget_options][][most_popular]",
                                        true,
                                        f.object.preferences.dig("pcb_budget_options", index, "most_popular") %>

我允许的参数如下所示

params.require(:price_template).permit(:currency_id,
                                         :program_id,
                                         :active,
                                         :default,
                                         country_ids: [],
                                         preferences: [budget_options:     [:amount, :most_popular, :text],
                                                       pcb_budget_options: [:amount, :most_popular]])

,它存储在数据库中,如下所示

   {
  "budget_options"=>[
    {"amount"=>"1.0", "text"=>"budget options"},
    {"amount"=>"2.0", "most_popular"=>"true", "text"=>"budget options"},
    {"amount"=>"3.0", "text"=>"budget options"}
  ],
  "pcb_budget_options"=>[
    {"amount"=>"1.0"},
    {"amount"=>"0.0"},
    {"amount"=>"-1.0", "most_popular"=>"true"}
  ]
}

,但 most_popular 值存储在字符串 格式,但我想将其存储为 布尔值

I'm getting a checkbox value from my form like this

<%= f.label 'Most Popular', class: "form-label" %>
                    <%= check_box_tag "price_template[preferences][pcb_budget_options][][most_popular]",
                                        true,
                                        f.object.preferences.dig("pcb_budget_options", index, "most_popular") %>

And the params I am permitting is like this

params.require(:price_template).permit(:currency_id,
                                         :program_id,
                                         :active,
                                         :default,
                                         country_ids: [],
                                         preferences: [budget_options:     [:amount, :most_popular, :text],
                                                       pcb_budget_options: [:amount, :most_popular]])

and it stored in DB like this

   {
  "budget_options"=>[
    {"amount"=>"1.0", "text"=>"budget options"},
    {"amount"=>"2.0", "most_popular"=>"true", "text"=>"budget options"},
    {"amount"=>"3.0", "text"=>"budget options"}
  ],
  "pcb_budget_options"=>[
    {"amount"=>"1.0"},
    {"amount"=>"0.0"},
    {"amount"=>"-1.0", "most_popular"=>"true"}
  ]
}

but the most_popular value is stored here is in string format but I want to store it as a boolean.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑色毁心梦 2025-01-26 21:42:40

JSONB 中不可能将其保存为DB中的“字符串”。

您可以创建自己的序列化器,以根据需要解析JSONB,或者只有布尔部分对您来说很重要,则可以在模型中创建一种方法,

class Mymodel
  def budget_most_popular
    ActiveRecord::Type::Boolean.new.cast(preferences["budget_options"]["most_popular"])
  end

  def pcb_budget_most_popular
    ActiveRecord::Type::Boolean.new.cast(preferences["pcb_budget_options"]["most_popular"])
  end
end

因此代码中的任何地方都可以通过调用此方法来获得真正的布尔值。

想象一下,您有以下记录

gt; MyModel.first
<MyModel:0x029I23EZ
id: 1,
name: "foo",
preferences: {
  "budget_options" => 
    [ 
      { "amount"=>"10", "most_popular"=>"true", "text"=>"" },
      {"amount"=>"0.0", "text"=>""}, {"amount"=>"0.0", "text"=>""}
    ],
  "pcb_budget_options"=>
    [
      {"amount"=>"20", "most_popular"=>"false"}, {"amount"=>"0.0"},
      {"amount"=>"0.0"}
    ]
}
...

,并且在代码中的某个地方您需要检查 forgive_option is must_popular

class MyModelController

   def show
     @mymodel = MyModel.first

     if @mymodel.budget_most_popular
       render "template/most_popular"
     else
       render "template/less_popular"
     end
   end
end

因为在我们的上一个记录 bucced_options as 中must_popular 设置为'true'我们的模型方法 buckit_mast_popular 将施放此'true''以返回布尔值 true 因此,我们的节目将渲染 most_popular template

这是一篇有趣的博客文章,内容涉及带有Rails的JSONB

It's not possible in jsonb it will be always save as a "string" in the DB.

You can create your own serializer to parse the jsonb as you want or if only the boolean part matter for you, you can create a method in your model like this

class Mymodel
  def budget_most_popular
    ActiveRecord::Type::Boolean.new.cast(preferences["budget_options"]["most_popular"])
  end

  def pcb_budget_most_popular
    ActiveRecord::Type::Boolean.new.cast(preferences["pcb_budget_options"]["most_popular"])
  end
end

So anywhere in your code you can get a real boolean value by calling this method.

Imagine you have the following record

gt; MyModel.first
<MyModel:0x029I23EZ
id: 1,
name: "foo",
preferences: {
  "budget_options" => 
    [ 
      { "amount"=>"10", "most_popular"=>"true", "text"=>"" },
      {"amount"=>"0.0", "text"=>""}, {"amount"=>"0.0", "text"=>""}
    ],
  "pcb_budget_options"=>
    [
      {"amount"=>"20", "most_popular"=>"false"}, {"amount"=>"0.0"},
      {"amount"=>"0.0"}
    ]
}
...

And somewhere in your code you need to check if budget_option is most_popular

class MyModelController

   def show
     @mymodel = MyModel.first

     if @mymodel.budget_most_popular
       render "template/most_popular"
     else
       render "template/less_popular"
     end
   end
end

Since in our last record budget_options as most_popular set as 'true' our model method budget_most_popular will cast this 'true'to return a boolean true so our show will render the most_popular template

Here is an interesting blog article about the jsonb with Rails

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文