FAQ Database Discussion Community


JSON data to instance variable in Ruby

ruby-on-rails,ruby,json,instance-variables,attr-accessor
This is my ruby code / JSON File. Three functions required, I have implemented the first two but am having trouble with the third one. I have only recently started learning ruby - any simplified explanations/answers are much appreciated class Company attr_accessor :jobs jobs = Array.new ## TODO: Implement this...

Adding additional data to an ActiveRecord object

ruby-on-rails,json,attr-accessor
I'm retrieving a "Widget" ActiveRecord object: @widget = Widget.find(params[:id]) I want to add some extra data to it before I return it, so I'm adding it using: @widget.display_name = "test display name" I can then do puts @widget.display_name and this indeed does display "test display name", but when I call...

Property from attr_accessor does not show when I render json

ruby-on-rails,ruby-on-rails-4,rails-activerecord,attr-accessor
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 ...

Using attr_accessor from inside class? [duplicate]

ruby,attr-accessor
This question already has an answer here: Why do Ruby setters need “self.” qualification within the class? 3 answers I'm trying to use an attr_accessor from inside the class where it is defined, to no avail. Why does this not work? I would expect the following to output "new"...