【问题标题】:How to save received string parameters in array field?如何将接收到的字符串参数保存在数组字段中?
【发布时间】:2011-11-13 12:03:24
【问题描述】:

如何从字符串参数中提取和保存数组?我正在尝试转换字符串 before_create 但这不起作用。当我评论 before_create :waypoints Mongoid 抛出错误时:

Parameters: {
    "utf8"=>"✓", 
     "authenticity_token"=>"nehoT1fnza/ZW4XB4v27uZsfFjjOu/ucIhzMmMKgWPo=", 
     "trip"=>{
         "title"=>"test", 
         "description"=>"test", 
         "waypoints"=>"[[52.40637,16.92517],[52.40601,16.925040000000003],[52.405750000000005,16.92493],[52.40514,16.92463],[52.404320000000006,16.924200000000003]]"
     }
}

Completed 500 Internal Server Error in 1ms

Mongoid::Errors::InvalidType (Field was defined as a(n) Array, but received a String with the value "[[52.40637,16.92517],[52.40601,16.925040000000003],[52.405750000000005,16.92493],[52.40514,16.92463],[52.404320000000006,16.924200000000003]]".):

编辑感谢您的帮助,现在可以使用,但我不知道以下方法是否好。我删除 before_create 并将参数名称从 waypoints 更改为 waypoints_s 并将 def waypoints 更改为 def waypoints_s:

#Parameters:
#"waypoints"=>"[[52.40637,16.92517],[52.40601,16.925040000000003],[52.405750000000005,16.92493],[52.40514,16.92463],[52.404320000000006,16.924200000000003]]"
"waypoints_s"=>"[[52.40637,16.92517],[52.40601,16.925040000000003],[52.405750000000005,16.92493],[52.40514,16.92463],[52.404320000000006,16.924200000000003]]"

class Trip
  include Mongoid::Document
  field :title, :type => String
  field :description, :type => String
  field :waypoints, :type => Array

  #before_create :waypoints

  #def waypoints=(arg)
  def waypoints_s=(arg)
    if (arg.is_a? Array)
      #@waypoints = arg
      self.waypoints = arg
    elsif (arg.is_a? String) 
      #@waypoints = arg.split(',')
      self.waypoints = JSON.parse(arg)
    else 
      return false 
    end 
  end
end

class TripsController < ApplicationController
  def create
    @trip = Trip.create(params[:trip])
    @trip.save
  end
end

【问题讨论】:

    标签: ruby arrays ruby-on-rails-3 mongoid


    【解决方案1】:

    将字符串解析为 JSON 对象:

    require 'json'
    
    waypoints = "[[52.40637,16.92517],[52.40601,16.925040000000003],[52.405750000000005,16.92493],[52.40514,16.92463],[52.404320000000006,16.924200000000003]]"
    JSON.parse(waypoints)
    
    => [[52.40637, 16.92517], [52.40601, 16.925040000000003], [52.405750000000005, 16.92493], [52.40514, 16.92463], [52.404320000000006, 16.924200000000003]]
    

    【讨论】:

    • 它在控制台中运行良好,但在 befor_create 方法中,rails 忽略它并保存所有数据而不使用航点。
    【解决方案2】:

    你需要使用序列化 http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-c-serialize

    此方法通过 YAML 格式将您的对象序列化到数据库(假设只是某种格式的文本)。

    class Trip < ActiveRecord::Base
    
      serialize :waypoints
    
    end
    
    trip = Trip.create( :waypoints => [[52.40637,16.92517],[52.40601,16.925040000000003],[52.405750000000005,16.92493],[52.40514,16.92463],[52.404320000000006,16.924200000000003]])
    
    Trip.find(trip.id).waypoints # => [[52.40637,16.92517],[52.40601,16.925040000000003],[52.405750000000005,16.92493],[52.40514,16.92463],[52.404320000000006,16.924200000000003]]
    

    【讨论】:

    • 我发现在 Mongoid 中我可以存储数组字段而无需序列化。我的问题仍然存在我认为以某种方式将航点参数从字符串转换为数组或删除引号?
    • serialize 方法正在为您进行转换,您只是像使用通常的数组一样使用航点。对我来说,使用序列化比使用 json 更简洁,因为你不需要解析,转换一些东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 2021-06-19
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多