0

How to extract and save array from string parameter? I'm trying convert string beafore_create but this doesn't work. When I comment before_create :waypoints Mongoid throw error:

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]]".):

EDIT Thanks for help, now it work but I don't know whether following approach is good. I remove before_create and change parameter name from waypoints to waypoints_s and def waypoints to 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

2 Answers 2

2

Parse the string as a JSON object:

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]]
Sign up to request clarification or add additional context in comments.

1 Comment

It works well in console but in befor_create method rails ignore it and save all data without waypoints.
1

You need to use serialize http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-c-serialize

This method serialize your object to database by YAML format (let's say just text with some format).

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]]

2 Comments

I found that in Mongoid I can store array fields without serialization. My problem still exist i think that some way convert waypoints parameter from string to array or remove quotes?
The serialize method is making converting for you, you're just using waypoints like it's usual array. For me using serialization is more neat way then using for example json, because you don't need to parse, to convert something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.