stillmotion
stillmotion

Reputation: 4628

Conditional JSON output for ActiveRecord Model (Rails 3)

I am using ActiveRecord's as_json integration with ActiveSupport::JSON to render custom output in my controllers. A basic setup I have in my model looks something like this:

def as_json(options = {})
  { :guid  => id,
    :title => title,
    :body  => body,
    :date  => created_at }
end

I want to take this setup a step further and show select information depending upon options passed. My question is, when I call respond_with @model_instance or render :json => @model_instance am I able to pass options that the options argument in as_json receives? If not, should I just create and convert a unique hash in my controller?

Upvotes: 0

Views: 1084

Answers (1)

zetetic
zetetic

Reputation: 47548

Seems like you could just call .as_json and pass in the options, no?

render :json => @mymodel.as_json(:someoption =>" value")

Upvotes: 1

Related Questions