Search code examples
ruby-on-railsruby-on-rails-4rails-activerecordattr-accessor

Property from attr_accessor does not show when I render json


In Rails, I have a User model. I added attr_accessor :scoreto the model.

If I render the JSON for one user, I do not see the "score" attribute. Why is this?

user = User.find(3)
user.score = 55
render json: user

Solution

  • render json: user, methods: [:score]
    

    attr_accessor is alternative for getter and setter method so it is a method and as I have mentioned we can call it as above