Sign in through GitHub

RailsCasts Pro episodes are now free!

Learn more or hide this

jasdeepjaitla's Profile

GitHub User: jasdeepjaitla

Comments by


I found a workaround, but still curious if anyone who is also using ActiveModel was having issues with as_json/to_json methods. Maybe this is the wrong forum, if so, please feel free to delete!


This is great! So I created a model and I am having a separate issue. Since I use the @errors = collection in my initializer and validators, when I generate JSON to store in my NOSQL database, it's including all kinds of stuff that I don't want to store:

{"errors"=>#, @username="jasdeep", @validation_context=nil, @firstname="Jasdeep", @lastname="Jaitla", @messages={}>, "username"=>"jasdeep", "email"=>... and so on

I've tried calling:

@user.as_json(only: [ :username, :email, :firstname, :lastname ])

as recommended in: -- ActiveModel::Serializers::JSON

but it has no effect, it still outputs every attribute and object. I have looked EVERYWHERE googling for hours upon hours and still can't find a solution for this.

I've tried overriding as_json in my Model class, but still no effect.

Any help or pointers are appreciated...

class User

include ActiveModel::Validations
include ActiveModel::Conversion
include ActiveModel::Serialization
extend ActiveModel::Callbacks
extend ActiveModel::Naming

attr_accessor :username, :email, :password, ... etc

def as_json(options={})
super(:only => [:username, :email])


Rails/ActiveModel 3.2.1
Ruby 1.9.3.p0