To_json failing with column named y

Found this: http://dev.rubyonrails.org/ticket/9797

However, doesn’t seem to solve my issue.

I run this:

Foo.find(:first).to_json(:include => { :bar => { :only =>
[:x,:y] } } )
or
Foo.find(:first).to_json(:include => :bar )

I get:

ArgumentError: wrong number of arguments (0 for 1)
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/serialization.rb:75:in y' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/ active_record/serialization.rb:75:insend’
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/serialization.rb:75:in `serializable_record’

These all run fine without issue:

Bar.find(:first).to_json
Foo.find(:first).to_json(:include => { :bar => { :only => [:x] } } )
Foo.find(:first).to_json

It’s very similar to the defect that was mentioned in the ticket,
however, the issue comes up when including the model.

Can anyone shed some light? Am I doing something wrong?

Thanks,

Roel

I’d suggest you actually want:

Foo.find(:first, {:include => {:bar => {:only => [:x, :y]}}).to_json

The :include optional param was in the wrong spot. It belongs inside
the parameter list of ActiveRecord::Base.find

Julian.

Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) NEW VIDEO
OUT 3rd APRIL
http://sensei.zenunit.com/

@Julian: Actually there is an :include option in both the find and
#to_json methods. With find it instructs ARec to construct a join and
bring back the associated model(s) in one db cycle. Similarly, in
to_json it instructs ARec to include the associations in the rendered
json. You might want to fact-check your ActiveRecord free-for-a-
limited-time video…

@Roel: I just tried the code below in my domain without problems.
Could the difference be that :include must name an association? In my
domain, User has_many :courts, :through=>:court_users

andy = User.find(:first, :include=>:courts)
andy.to_json :except=>[:crypted_password, :salt],
:include=>{:user_cash_adjustment_account=>{:only=>:opening_balance}}
#=>{
“updated_at”:“2008/04/01 09:18:44 -0400”,
“activated_at”:null,
“deleted_at”:null,
“remember_token_expires_at”:“2008/04/15 13:18:44 -0400”,
“activation_code”:null,
“id”:1,
“courts”:[{
“jems_code”:“GVL”},{
“jems_code”:“SPG”}
],
“default_court_id”:2,
“remember_token”:“d1956c81b94199ee9ad06b6e82f87e44490ad86f”,
“login”:“AndyV”,
“state”:“active”,
“email”:"…andy…",
“created_at”:“2007/09/05 08:33:49 -0400”,
“active”:true}

Right you are!

BTW, It’s actually a general “Rails” set of videos, and it’s pretty
basic - nothing about JSON in it :stuck_out_tongue:

You’re right, include must name an association, just like in
AR::Base#find

Have a look at the bottom part of:
http://api.rubyonrails.org/classes/ActiveRecord/Serialization.html#M001135

:only is to specify the fields returned.

Julian.

Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) NEW VIDEO
(#2) OUT NOW!
http://sensei.zenunit.com/

Sorry, I thought you were just being very generic… not actually
asking about a column specifically named ‘y’.

Thanks Julian and Andy for the response.

Here are my associations:
Foo has_one Bar
Bar belongs_to Foo

I am able to use to_json method if I only specify to include column x
from the bar object. If i include column y, that is when I get error.

Is there an issue using column named “y”?

From the link I provided above, it states this “Add
“Model.define_attribute_methods” to your rails after_initialize
callback so the “y” accessor is automatically generated.”

Where would I add that? To my model?

Any help is much appreciated.

Roel

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs