Simon M. Simon M. - 4 years ago 82
Ruby Question

Add temporary attribute to a model

I start to learn ruby on rails and I want to temporary add an attribute to a model so I added the accessor to my model :

attr_accessor :dettes

And then I used the method
, here is what I have :

@depenses = Depense.where(user_id:
@depenses.each do |d|
d.dettes = "value"
render json: @depenses

But this didn't work, my new attribute isn't added

I've seen others questions about this but I'm still not able to resolve this, am I missing something ?

Answer Source

By default, render :json only adds the permanent attributes. You can add the temporary one as well this way:

render json: @depenses, methods: :dettes
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download