27 Nov '12, 6pm

Cleaner serialization and the perils of overriding as_json:

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...

Tweets

Roam7 is a much better way to get #travel reviews than #TripAdvisor.

Roam7 is a much better way to get #travel revie...

e27.sg 27 Nov '12, 5am

Essentially, Roam7 turns your friends’ travel photo albums on Facebook into beautiful travel stories and transforms the tr...