Displaying 20 results from an estimated 30000 matches similar to: "Help with JSONified objects from ActiveRecord"
2005 Jul 09
0
ActiveRecord JSON Objects
Someone asked me in IRC if javascript can access ActiveRecord
properties. At first, this seemed like a silly request. But then I
remember JSON and thought, why can''t you?
I implemented a quick ActiveRecord -> JSON converter using the ruby-json gem.
http://rafb.net/paste/results/Yixi5I21.html
One problem was that I have to call .to_s to convert everything to a
format for JSON.
2009 Jan 21
3
Nested serialization with to_json and to_xml with array and hash element
Title: Nested serialization with to_json and to_xml with array and
hash element
class Person < ActiveRecord::Base
has_one :address
has_one :job
def office()
return "Executive_Suite"
end
end
class Address < ActiveRecord::Base
belongs_to :person
end
class Job < ActiveRecord::Base
belongs_to :person
has_one :paygrade
def Title
return "Engineer"
end
end
2009 Oct 05
0
ActiveRecord Polymorphic Inheritance
I am using RoR ActiveRecord polymorphic inheritance and was wondering
if it''s possible to access the base class'' association methods in the
invocation of either to_xml or to_json without having to depict the
base class data in the serialized string. Here are my classes:
# base class
class Asset < ActiveRecord::Base
belongs_to :resource, :polymorphic => true
2011 Apr 17
0
newbie: to_json only/except + association confusion
hi,
i want to return to_json with ''only'' and also filtered.so i tried
various ways, but none of them are really good:
A)#OK , but filter missing
format.json { render :json => @project.to_json(:only =>
[:title ], :include=> { :tasks => {:only=>[:id,:title] } } ) }
B)#Not OK > filter does not work,
format.json { render :json => @project.to_json(:only
2009 Aug 07
0
How to specify ActiveRecord's to_json encoding
Question:
Hi, our company is using Ruby 1.8.6 with Rails 2.2.2. Does anyone
know we can explicitly specify what encoding to use when calling
to_json() or to_xml() methods?
Problem:
We have some multibyte characters in our database. For example we
have a table with a name column that has this French accented e: Café
Records. When we serialize this object using ActiveRecord''s to_xml()
2007 Oct 04
4
2 more JSON / XML feature parity patches before Rails 2.0
I''d really like to see just 2 more functional changes to the JSON
serialization/encoding before we get to Rails 2.0.
First, and I think this is really crucial because without it all the
efforts to output JSON from ActiveRecord objects would have been half
in vain, is to allow us to do this in controllers:
@authors = Author.find(:all)
render :json => @authors.to_json(:only =>
2012 Jan 27
2
to_json performance
Originally posted on github, reported to the right place.
I would like to open a discussion about how `to_json` and `as_json`
operates in Rails from a performance standpoint.
I''m using Rails 3.2 but this issue applies to almost all versions of
Rails.
Our use case presents the challenge in sending out potentially large
JSON (or XML, but we''ll focus on JSON rendering here)
2007 Dec 11
0
help for hash.to_json
hi , all
under rails 2.0
aotianlong@aotianlong:~/workspace/killer$ ruby script/console
Loading development environment (Rails 2.0.1)
>> {:user=>"aotianlong"}.to_json
=> "{\"user\": \"aotianlong\"}"
>> user = User.find :first
=> #<User id: 1, uname: "aotianlong">
>> user.to_json
=>
2007 Oct 23
2
to_json inconsistency?
Hello,
ActiveSupport::JSON::Variable.to_json is the only to_json without an
"options" parameter. Is this intended?
For example, this code in a .rjs
page[@user.dom_id].visual_effect :drop_out, :afterFinish =>
ActiveSupport::JSON::Variable.new("foo")
fails with
ActionView::TemplateError: wrong number of arguments (1 for 0)
On line #7 of
2007 Mar 24
0
overloading to_json with JSON gem installed causing error
I installed the JSON gem because I wanted the JSON parser.
Before installing the JSON gem, overloading worked fine. Before I
remove it, I''d like to see if anyone has an idea of what is going on.
It''s the only JSON parser I''m aware of, so it would be a bummer if it
didn''t work.
After installing, if I overload the to_json method in the class, it
gives me the
2007 Aug 07
2
"badly formed JSON" exception
I tried to implement the "Rendering JSON in actions" example from
http://wiki.rubyonrails.com/rails/pages/HowtoGenerateJSON :
@headers["Content-Type"] = "text/plain; charset=utf-8"
data = { :foo => ''bar'', :etc => ''rez'' }
render :text => data.to_json
When I try to decode this in javascript with
2008 Jul 12
0
to_json in Rails 2.0.2 not generating proper json?
I am using Rails 2.0.2 and trying to use to_json to generate json. I
notice several problems.
1) datetime field such as created_at mapped to "created_at": {}
2) :except does not work, @post.to_json(:except =>
[:created_at, :updated_at] still gives me "created_at": {},
"update_at": {}
3) problem with escaped char mapping: ''<'' mapped to
2010 Apr 05
2
Harmonizing JSON/XML serialization
The way Rails handles root nodes in JSON and XML serialization is
inconsistent. This has been discussed before:
https://rails.lighthouseapp.com/projects/8994/tickets/2584-232-activeresource-json-doesnt-send-parameters-with-root-node
This seems mostly taken care of with
ActiveModel::Base.include_root_in_json, especially if/when it ends up
in ActiveResource. However, there is also the issue of how
2010 Jul 05
0
Serialized JSON object importation
Hi,
I would like to import a serialized object (from a JSON export) inside
my database.
It work fine if for instance I do:
>> l = Yea.create(:title => "foo bar")
=> #<Yea id: 3, title: "foo bar", created_at: "2010-07-05 21:44:54",
updated_at: "2010-07-05 21:44:54">
>> j = l.to_json
=>
2011 Aug 25
7
How to safely embed JSON object in HTML document
Hi all,
I''m working on a Backbone.js single page app with Rails 3.1, and in an
attempt to save on HTTP requests, I want to embed initial data set in
a HTML document that is sent back to the browser after successful
login.
I was thinking I can simply convert my ruby object to JSON, then HTML
escape resulting string of JSON, and then use that as a value for
JavaScript variable. Something
2009 Dec 08
0
ActiveSupport 2.3.5 and json 1.2.0 gem
Hey,
I''ve tried every trick I can find, yet I''m still unable to get the
json gem and ActiveSupport to play nice.
No matter in which order I load json and active support, I still end
up with: NoMethodError: undefined method `[]'' for
#<JSON::Ext::Generator::State:0x104b7fac0>
I''m trying to use the JSON object directly, *not* as a ActiveSupport backend:
2006 May 27
5
ActiveSupport JSON working?
Hi,
I am trying to convert a hash to JSON using ActiveSupports built in
.to_json. I am doing similar to the example below but are there
certain strings that could go in place of ''bar'' or ''rez'' that will
break the to_json method? I am using a very long string from rjs
output in place of ''bar''.
Perhaps .to_json is not ready yet?
Thanks,
Peter
2007 Dec 27
5
Rails 2.02 dropped JSON attribute tag ?
Hey there
I was scratching my head over why my application breaks when I run on
a local server but works on my remote server. It turns out that my
local server is Rails 2.0.2 and my remote server is 1.2.3 and I expect
the tag "attributes" to be passed with a json stream, before parsing.
Unfortunately, this was dropped in 2.0.2. Is there any documentation
on this? Any way to get it back?
2008 Nov 20
0
pass json data into javascript
Hello from a Ruby on Rails beginner!
I''ve been trying to pass some data to a variable in Javascript by using
json. Seems like the Javascript variable doesn''t receive the data
correctly.
This is the code in controller. @courses variable find the data from
database.
class CoursesController < ApplicationController
def index
@courses = Course.find(:all)
end
end
This
2010 Feb 20
1
Advanced use of to_xml and to_json
Hello,
I''m working on a Rails app which has a REST API. I manage two formats
JSON and XML.
Very often to simplify the use if the API, I''m making includes. But when
you add params to to_json and to_xml like :only, :except, it''s not only
applied on the root object but on all the objects included.
Do you know libs or methods to answer to these different problems:
* Tell