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

Getting the serialized hash without render #1101

Closed
PericlesTheo opened this issue Aug 30, 2015 · 8 comments
Closed

Getting the serialized hash without render #1101

PericlesTheo opened this issue Aug 30, 2015 · 8 comments

Comments

@PericlesTheo
Copy link
Contributor

Hey humans,

Let's say I have

class MessageSerializer < ActiveModel::Serializer
  attributes :content

  belongs_to :user
end

When I use render json: @message in the controller I get {:message=>{:content=>"some peoisadfnsaidfnple like wefaefsushi", :user=>{:email=>"[email protected]"}}} which is what I'd expect.

However, I'd like to be able to get the same serialization without using render in the controller.

I couldn't find anything in the docs and the only way around I found is

ActiveModel::Serializer::Adapter::Json.new(MessageSerializer.new(Message.last)).serializable_hash

Is there an easier way of achieving the above?

Cheers!

@PericlesTheo
Copy link
Contributor Author

I'm following the master branch currently on 0.10.0.rc2

I had a look before in the SerializableResource but probably I missed something as I just realized one can use ActiveModel::SerializableResource.new(Message.last).serializable_hash instead.

Should a reference of the above be added to the documentation? It would have saved me quite some time :)

@bf4
Copy link
Member

bf4 commented Aug 30, 2015

Yes please!

B mobile phone

On Aug 30, 2015, at 4:19 AM, Pericles Theodorou [email protected] wrote:

I'm following the master branch currently on 0.10.0.rc2

I had a look before in the SerializableResourcebut probably I missed something as one can useActiveModel::SerializableResource.new(Message.last).serializable_hash` instead.

Should a reference of the above be added to the documentation? It would have saved me quite some time :)


Reply to this email directly or view it on GitHub.

@beauby
Copy link
Contributor

beauby commented Sep 1, 2015

Should we close this issue now that the question seems to have been answered?

@PericlesTheo
Copy link
Contributor Author

Yeap, I will create a PR for hopefully today. Cheers

@joaomdmoura
Copy link
Member

Nice, I glad that you find your way through it @PericlesTheo, Would be nice to have it on our new docs as well.

@PericlesTheo
Copy link
Contributor Author

I know I'm terrible :(

I should have done it last week.

I'll try tonight

@joaomdmoura
Copy link
Member

haha np, tks! 😄

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

4 participants