Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get to_json to behave without render? #1487

Closed
dudo opened this issue Jan 30, 2016 · 6 comments
Closed

How to get to_json to behave without render? #1487

dudo opened this issue Jan 30, 2016 · 6 comments

Comments

@dudo
Copy link

dudo commented Jan 30, 2016

In my controller, I can convince the app to use my serializers:

def show
  @user = User.find_by_id(params[:id])
  render json: @user
end

But for the life of me, I can't seem to figure out how to get the object to use the serializer on command. I've tried the following to no avail:

@user.to_json
@user.as_json
@user.serializable_hash

UserSerializer.new(@user)
UserSerializer.new(@user).attributes (this almost works, but I have a has_many that I can't get to show up)

What am I missing?? This is driving me nuts.

@domitian
Copy link
Contributor

domitian commented Feb 1, 2016

You can't, as it is mentioned in this document Basic Serializer in design docs.

Rails will transparently use your serializer when you use render :json in your controller.

So it won't use your serializers as_json and serializable_hash methods when you are calling from the console, it will by default use the active record methods

@bf4
Copy link
Member

bf4 commented Feb 1, 2016

@dudo What version of AMS are you on? https://github.com/rails-api/active_model_serializers/blob/master/CONTRIBUTING.md

The docs describe how to serialize outside a controller https://github.com/rails-api/active_model_serializers/blob/master/docs/howto/outside_controller_use.md

@domitian the file you linked is the original design doc from 2011

@dudo
Copy link
Author

dudo commented Feb 3, 2016

0.10.0.rc4

UserSerializer.new(User.first).as_json
  User Load (0.9ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT $1  [["LIMIT", 1]]
=> {"object"=>{"id"=>1, "name"=>"Brett Dudo", "plaid_public_token"=>nil, "created_at"=>Thu, 21 Jan 2016 05:28:45 UTC +00:00, "updated_at"=>Wed, 03 Feb 2016 05:27:23 UTC +00:00, "email"=>"[email protected]", "public_token"=>"..."}, "instance_options"=>{}, "root"=>nil, "scope"=>nil}
{
  id: 1,
  name: "Brett Dudo",
  email: "[email protected]",
  monthly_income_amount: [redacted],
  monthly_available_balance: [redacted],
  current_transactions: [
    {
      id: "jbA5MZNzoospaQ0kyAmjTaL0p598pvHZr5Oqz",
      name: "Flyers",
      amount: 21.17
    },
    {
      id: "rJX1EOzL44SneY4NbAwmUkvby1gDy8FZpNEOy",
      name: "Target",
      amount: 146.45
    },
    ...
  ]
}
class UserSerializer < ActiveModel::Serializer
  attributes :id, :name, :email, :monthly_income_amount, :monthly_available_balance

  has_many :current_transactions
end

Hopefully that describes the mismatch. I'm using Devise, not sure if that messes with serialization. Also, plaid_public_token is on there, which I'm not sure how... the column is actually just public_token.

@bf4
Copy link
Member

bf4 commented Feb 3, 2016

Yeah, that's not the way to do it. The json gem adds methods all ruby objects. I am thinking of moving making the serializer behave like the attributes adapter when calling as_json on it. In any case, see #1487 (comment) where I ref'd what you need to do

@dudo
Copy link
Author

dudo commented Feb 3, 2016

To be clear, that's the instruction from /outside_controller_use.md, yes?

AHHHHHH. I was looking at it the whole time. It says ActiveModel::SerializableResource but I was just seeing the name of my Serializer. Dumb... Thank you for clarifying.

@dudo dudo closed this as completed Feb 3, 2016
@bf4
Copy link
Member

bf4 commented Feb 3, 2016

no prob. Glad it's figured out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants