views:

485

answers:

2

Hi,

I have the following code

@asset = Asset.first(:include => [:asset_statuses => [:asset_status_name, {:asset_location => [{:asset_floor => :asset_building}]}],:asset_type => [:asset_category => :asset_department]],

(probably not the best DB table desing but that's what I have to use...)

So, the Asset.first works correctly and it brings back the data correctly but when I try to use the same :include in to_json method if fails with the followings error:

@asset.to_json( :include => [:asset_statuses => [:asset_status_name, {:asset_location => [{:asset_floor => :asset_building}]}],:asset_type => [:asset_category => :asset_department]] )

NoMethodError (undefined method `macro' for nil:NilClass):

The json method has the same :include syntax as find, should it work in both?

Thanks - Erik

+2  A: 

I think the to_json :include syntax is a little different.

I usually do

@asset.to_json(:include => { :asset_statuses => {
                             :include => :asset_status_name}})

(and start with a small one then add all of the other stuff. annoying schema!)

MattMcKnight
+1  A: 

In case anyone else runs into a bizarre related issue that I did...to_json will also return this error when you try to include an association that is not defined on the model.

floyd