首页 > 解决方案 > 如何在 Rails 控制台中的某个值之间添加值

问题描述

我的数据库中有以下记录

[662] #<ChapterSolution:0x000055ec31cdfb40> {
            :id => 5071,
    :chapter_id => 221,
    :created_at => Tue, 19 Sep 2017 18:24:57 IST +05:30,
    :updated_at => Sat, 02 Dec 2017 10:24:53 IST +05:30,
      :question => "11",
          :part => "i",
        :answer => "See Explanation",
      :solution => "<img src='//cdn.google.in/editor/pictures/573/content.jpeg' />"

[663] #<ChapterSolution:0x000055ec31cdfb40> {
            :id => 5071,
    :chapter_id => 221,
    :created_at => Tue, 19 Sep 2017 18:24:57 IST +05:30,
    :updated_at => Sat, 02 Dec 2017 10:24:53 IST +05:30,
      :question => "11",
          :part => "i",
        :answer => "See Explanation",
      :solution => "<img src='//cdn.google.in/editor/pictures/574/content.jpeg' />"

[664] #<ChapterSolution:0x000055ec31cdfb40> {
            :id => 5071,
    :chapter_id => 221,
    :created_at => Tue, 19 Sep 2017 18:24:57 IST +05:30,
    :updated_at => Sat, 02 Dec 2017 10:24:53 IST +05:30,
      :question => "11",
          :part => "i",
        :answer => "See Explanation",
      :solution => "<img src='//cdn.google.in/editor/pictures/575/content.jpeg' />"

我想在每条记录的解决方案值中添加https:img src='//cdn.google...之间,以使每条记录中的 URL 正确,

但是我不知道该怎么做,任何帮助/建议将不胜感激。

标签: sqlruby-on-railspostgresqlrails-console

解决方案


一种便宜的方法是使用gsub和正则表达式:

object[:solution].gsub(/img src='\/\//,"img src='https://")

gsub 文档


推荐阅读