Roam7 is a much better way to get #travel revie...
Essentially, Roam7 turns your friends’ travel photo albums on Facebook into beautiful travel stories and transforms the tr...
November 27, 2012 thegrantovich rails serialization object-oriented programming Better serialization, less as_json Suppose you have the following in your Rails app: # app/models/user.rb class User < ActiveRecord::Base has_secure_password has_many :posts end # app/models/post.rb class Post < ActiveRecord::Base belongs_to :user end # app/controllers/posts_controller.rb class PostsController < ApplicationController def show @post = Post.find(params[:id]) render json: @post.as_json( only: [:id, :content, :created_at], include: { user: { only: [:id, :username] } } ) end end Your show action returns a JSON representation of a post, along with some limited information about the user it belongs to, using as_json . Works like a champ. However… that nested hash you’re passing into as_json is a bit clunky. It’ll get clunkier if you add more associations, and you’ll have to repeat you...
Full article: http://robots.thoughtbot.com/post/36676073713/better-seri...
@netwillnet
»
25 Dec '12, 1pm
@stackful_io
»
30 Nov '12, 10am
@sfsekaran
»
29 Nov '12, 7pm
@bizarre07
»
28 Nov '12, 12am
@sungheeekang
»
27 Nov '12, 11pm
@ischroedi
»
27 Nov '12, 7pm
@JetBlackMeerkat
»
27 Nov '12, 8pm
Essentially, Roam7 turns your friends’ travel photo albums on Facebook into beautiful travel stories and transforms the tr...