jenna_s
unread,May 12, 2012, 5:27:00 PM5/12/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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