Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails to_json problem with :include

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)

The Asset.first works correctly and it brings back the data correctly but when I try to use the same :include in the to_json method it 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 to_json method has the same :include syntax as find; I don't understand why it is not working.

like image 836
Erik Avatar asked Dec 18 '09 19:12

Erik


1 Answers

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.

like image 123
tfwright Avatar answered Oct 06 '22 23:10

tfwright