Sorry for the newbie question but still very new to Ruby and Mongo.
Not 100% sure what I am missing, but how would I get just the value back
from “url” in the following json result?
[{"url":"www.google.com"}]
So I want something like if I do a
puts url_var
it should just display http://www.google.com
MongoMapper code to get URl from Mongo
redirect_url = Surl.all(:url_key => "#{urlkey}")
myjson = redirect_url.to_json(:only => [:url])
puts redirect_url
gives me
[{"url":"www.google.com"}]
if I do something like
redirect_url.each do |key,value|
puts key
puts value
end
key is still the full json string and value is empty.
Surely I must be missing something really basic here
Okay, I assume
Surlis a model of yours, which has at least these fieldsurl_keyandurl, so to access the url you don’t have to convert the result to json and then fetch to key, but you can access it directly.Surl.allreturns an array (check the mongo mapper docs), if you want a single result useSurl.find_by_url_key(urlkey). Now you can access the url directly by usingputs redirect_url.urlBe sure to read up on http://mongomapper.com/documentation/plugins/dynamic-querying.html and http://mongomapper.com/documentation/plugins/querying.html, to understand the results and differences.