Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: attr_accessor doesn't show up when try to render as JSON

In my application I have a model that is below;

class Init < ActiveRecord::Base
  attr_accessor :clientAvailability
  attr_accessible :returnCode, :returnMessage, :tokenCode, :updateURL
end

In here when I've added the **attr_accessor** later on and when I try to apply **render json: init** it doesn't show the **clientAvailability** property even though I've set it as;

init=Init.new
init.clientAvailability="3"

When I debug it I can see that it is set, but doesn't simply show up when I try to render it as json. What could be the thing I'm missing?

like image 576
Ali Avatar asked Dec 09 '22 20:12

Ali


1 Answers

Is clientAvailability a column in the DB? If not, then I suppose it is the default way - to serialize only the DB attributes. But you can do:

render json: init, methods: [:clientAvailability]

to include clientAvailability attribute.

like image 176
khustochka Avatar answered Mar 05 '23 22:03

khustochka