I have a model setup like so:
class Item < ActiveRecord::Base
belongs_to: media
def as_json(options={})
super(:include => :media)
end
end
class Media < ActiveRecord::Base
def media_func(some_param)
...
some_val
end
def as_json(options={})
output = super(:include => :media)
output[:some_attr] = media_func(options[:some_param])
output
end
end
I have a controller like this:
class Api::FeedsController < ApplicationController
def item_list
@some_param = some_parameter
@item_list = Item.all
render :json => @item_list
end
end
How the heck do I pass @some_param so that the media object receives it when rendering to json?
Just add this to the controller:
render :json => @item_list.as_json(:some_param => @some_param)Edit. Added the method as_json to the object.