as_json include carrierwave model

465 views
Skip to first unread message

jenna_s

unread,
May 12, 2012, 5:27:00 PM5/12/12
to carrierwave
Hi,

I'm not sure if this is the right newsgroup to ask a json
serialization question, but there's something that's strange with the
my carrierwave model. I have a basic setup where a user has many
posts, and a user has a profile_photo uploader. I need to return posts
created by various users as json. I also want to include only certain
properties of users which also includes the profile_photo. So,
something like this works:
format.json { render :json =>
{
:posts => @posts.as_json(:include =>
{:user =>
{:only =>
[:username, :gender, :profile_photo]}})
}

returns
{"posts":[{"post":
{"content":"foobar","created_at":"2012-05-12T19:31:19Z","id":
163,"updated_at":"2012-05-12T19:31:19Z","user_id":3,"user":
{"gender":"","profile_photo":{"url":"/uploads/user/profile_photo/3/
IMG_2942.jpg","thumb":{"url":"/uploads/user/profile_photo/3/
thumb_IMG_2942.jpg"}},"username":"abcdef"}}}]}

If I try to do select only a few properties of :profile_photo, as
such:
format.json { render :json =>
{
:posts => @posts.as_json(:include =>
{:user =>
{:only =>
[:username, :distance, :profile_photo =>
{:only => :url}]}})
}

returns
{"posts":[{"post":
{"activities":"foobar","created_at":"2012-05-12T19:31:19Z","id":
163,"updated_at":"2012-05-12T19:31:19Z","user_id":3,"user":
{"age":null,"gender":"","username":"abcdef"}}}]}

Instead of url, I tried full_url but that didn't help. I guess I don't
understand how as_json works under the hood...

Anyone has any suggestions?

Thanks,
Jenna

Jacob Tjørnholm

unread,
May 13, 2012, 2:26:11 AM5/13/12
to carri...@googlegroups.com
Hi, 

Carrierwave implements it's own version of as_json which doesn't support the same options as the ActiveModel versions. Maybe it should in the future, but meanwhile you need some workaround. 

One idea would be to make the profile photo url available on the User model as a helper method: 

class User
  ...
  def profile_photo_url
    profile_photo.url
  end
  ...
end

You could then include it in the json by using the :methods option: 

format.json { render :json =>
         {
           :posts => @posts.as_json(:include =>
             {:user =>
               {:only =>
                 [:username, :distance],
                :methods => :profile_photo_url }})
         }
}

This should add the URL under the key "profile_photo_url" in the user object. 

/Jacob





--
You received this message because you are subscribed to the Google Groups "carrierwave" group.
To post to this group, send email to carri...@googlegroups.com.
To unsubscribe from this group, send email to carrierwave...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/carrierwave?hl=en.


jenna_s

unread,
May 13, 2012, 10:33:58 PM5/13/12
to carrierwave
Thanks for the advice. That works. I'm good with this hack for now.

J

Chris Moody

unread,
Mar 22, 2016, 10:32:28 PM3/22/16
to carrierwave
Not to be four years late to the party, but considering this problem still exists and I have stumbled across this board every time I have forgotten how carrierwave implements as_json:  Just override the serializable_hash method in your uploader (instead of overriding the as_json method).  I always want it to have the name so this is my goto:
  def serializable_hash(options = nil) {name: name, location: location, url: url} end

I am sure the op is long done with this concern, but hopefully anyone who stumbles across this issue in the future will find this useful.
Reply all
Reply to author
Forward
0 new messages