# To\_json failing with column named y

**URL:** <https://www.ruby-forum.com/t/to-json-failing-with-column-named-y/133643>\
**Category:** Rails\
**Created:** [April 2, 2008, 8:26am UTC](https://www.ruby-forum.com/t/to-json-failing-with-column-named-y/133643 "2008-04-02T08:26:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@Roel](https://www.ruby-forum.com/u/Roel)\
**Post date:** [April 2, 2008, 8:26am UTC](https://www.ruby-forum.com/t/to-json-failing-with-column-named-y/133643/1 "2008-04-02T08:26:40Z")

</div>

Found this: [http://dev.rubyonrails.org/ticket/9797](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:in `send’  
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

---

<div class="post-metadata">

**Author:** ![Roel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@Roel](https://www.ruby-forum.com/u/Roel)\
**Post date:** [April 2, 2008, 8:55am UTC](https://www.ruby-forum.com/t/to-json-failing-with-column-named-y/133643/2 "2008-04-02T08:55:03Z")

</div>

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/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![Roel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@Roel](https://www.ruby-forum.com/u/Roel)\
**Post date:** [April 2, 2008, 3:54pm UTC](https://www.ruby-forum.com/t/to-json-failing-with-column-named-y/133643/3 "2008-04-02T15:54:31Z")

</div>

@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}

---

<div class="post-metadata">

**Author:** ![Roel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@Roel](https://www.ruby-forum.com/u/Roel)\
**Post date:** [April 3, 2008, 10:21am UTC](https://www.ruby-forum.com/t/to-json-failing-with-column-named-y/133643/4 "2008-04-03T10:21:11Z")

</div>

Right you are!

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

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

Have a look at the bottom part of:

> **[ActiveRecord::Serialization](https://api.rubyonrails.org/classes/ActiveRecord/Serialization.html#M001135)**
>
> Active Record Serialization.

: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/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![Roel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@Roel](https://www.ruby-forum.com/u/Roel)\
**Post date:** [April 3, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/to-json-failing-with-column-named-y/133643/5 "2008-04-03T16:05:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Roel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@Roel](https://www.ruby-forum.com/u/Roel)\
**Post date:** [April 3, 2008, 2:44am UTC](https://www.ruby-forum.com/t/to-json-failing-with-column-named-y/133643/6 "2008-04-03T02:44:23Z")

</div>

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
