Rails Serialize: From YAML to JSON - Getting "unexpected token" error -
i used have following code save array of "friends" ids (the facebook user ids imported via graph api , omniauth) user model:
class user < activerecord::base ... serialize :friends def self.from_omniauth(auth) require 'net/http' require 'json' where(auth.slice(:provider, :uid, :email, :image)).first_or_initialize.tap |user| user.provider = auth.provider user.uid = auth.uid user.oauth_token = auth.credentials.token friendsinfo = net::http.get(uri("https://graph.facebook.com/" + auth.uid + "/friends?fields=installed&access_token=" + user.oauth_token)) unless friendsinfo == nil friendsdata = (json.parse(friendsinfo))["data"] farr = [] friendsdata.each |fd| if fd["installed"] == true farr.push(fd["id"]) end end end user.friends = farr user.save! end end
this code saves user ids of facebook friends have app installed array, i.e. ["3232344", "2342434", "8738453"]
.
however, decided change serialize :friends
serialize :friends, json
security issues, per codeclimate's advice, either:
a) saves 1 json string (if there multiple "friends"), or
b) gives 795: unexpected token @ ''[user id here]' - ---'
error if there 1 "friend" has app installed.
this answer has comment asking how make work in rails 4, i'm guessing adding json
worked in rails 3.1 doesn't more? i've tried variations of to_json
, activesupport::json.decode
, etc., , writing custom serializer, guess error serializing after creating array in current code? callback answer?
i found think think converting serialized yaml serialized json in rails models
i not seeing calling yaml or getting 1 code.
but here magic line yaml.load(medium.send(:attribute, :payload)).to_json
i hope help.
Comments
Post a Comment