Skip to content

Instantly share code, notes, and snippets.

@sergio-fry
Last active January 25, 2019 06:56
Show Gist options
  • Save sergio-fry/a72a98cc57a22ba5e65dbece111d19d5 to your computer and use it in GitHub Desktop.
Save sergio-fry/a72a98cc57a22ba5e65dbece111d19d5 to your computer and use it in GitHub Desktop.
ActiveRecord JSON examples
User.limit(2).as_json
# User Load (2.0ms) SELECT "users".* FROM "users" LIMIT ? [["LIMIT", 2]]
#=> [{"id"=>1, "name"=>"Анна Терентьева", "created_at"=>Wed, 23 Jan 2019 21:47:37 UTC +00:00, "updated_at"=>Wed, 23 Jan 2019 21:47:37 UTC +00:00, "photo_url"=>nil, "nickname"=>nil, "first_name"=>nil, "last_name"=>nil, "about"=>nil, "birthday"=>nil}, {"id"=>2, "name"=>"Абрамова София", "created_at"=>Wed, 23 Jan 2019 21:47:38 UTC +00:00, "updated_at"=>Wed, 23 Jan 2019 21:47:38 UTC +00:00, "photo_url"=>nil, "nickname"=>nil, "first_name"=>nil, "last_name"=>nil, "about"=>nil, "birthday"=>nil}]
User.select(:id, :name).limit(2).as_json
# User Load (1.4ms) SELECT "users"."id", "users"."name" FROM "users" LIMIT ? [["LIMIT", 2]]
#=> [{"id"=>2, "name"=>"Абрамова София"}, {"id"=>20, "name"=>"Ангелина Нестерова"}]
User.select(:id, :name).where('start_at > ?', Time.now).limit(2).as_json
# User Load (1.6ms) SELECT "users"."id", "users"."name" FROM "users" WHERE (start_at > '2019-01-25 06:50:27.895229') LIMIT ? [["LIMIT", 2]]
#ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: start_at: SELECT "users"."id", "users"."name" FROM "users" WHERE (start_at > '2019-01-25 06:50:27.895229') LIMIT ?
# from (irb):10
User.select(:id, :name).joins(:events).where('start_at > ?', Time.now).limit(2).as_json
# User Load (1.9ms) SELECT "users"."id", "users"."name" FROM "users" INNER JOIN "events" ON "events"."user_id" = "users"."id" WHERE (start_at > '2019-01-25 06:51:12.674080') LIMIT ? [["LIMIT", 2]]
#=> [{"id"=>1, "name"=>"Анна Терентьева"}, {"id"=>1, "name"=>"Анна Терентьева"}]
User.select(:id, :name).limit(2).map { |u| { user_id: u.id, time: Time.now, foo: [:bar] } }.as_json
# User Load (0.7ms) SELECT "users"."id", "users"."name" FROM "users" LIMIT ? [["LIMIT", 2]]
#=> [{"user_id"=>2, "time"=>"2019-01-25T06:55:16.845+00:00", "foo"=>["bar"]}, {"user_id"=>20, "time"=>"2019-01-25T06:55:16.845+00:00", "foo"=>["bar"]}]
User.select(:id, :name).joins(:events).limit(2).map { |u| { user: u, events: u.events.select(:title, :start_dt).limit(2) } }.as_json
# User Load (1.7ms) SELECT "users"."id", "users"."name" FROM "users" INNER JOIN "events" ON "events"."user_id" = "users"."id" LIMIT ? [["LIMIT", 2]]
# Event Load (1.5ms) SELECT "events"."title", "start_dt" FROM "events" WHERE "events"."user_id" = ? LIMIT ? [["user_id", 1], ["LIMIT", 2]]
# Event Load (1.0ms) SELECT "events"."title", "start_dt" FROM "events" WHERE "events"."user_id" = ? LIMIT ? [["user_id", 1], ["LIMIT", 2]]
#=> [{"user"=>{"id"=>1, "name"=>"Анна Терентьева"}, "events"=>[{"id"=>nil, "title"=>"Accusamus in sunt quos.", "\"start_dt\""=>"start_dt"}, {"id"=>nil, "title"=>"Corporis excepturi quisquam ea.", "\"start_dt\""=>"start_dt"}]}, {"user"=>{"id"=>1, "name"=>"Анна Терентьева"}, "events"=>[{"id"=>nil, "title"=>"Accusamus in sunt quos.", "\"start_dt\""=>"start_dt"}, {"id"=>nil, "title"=>"Corporis excepturi quisquam ea.", "\"start_dt\""=>"start_dt"}]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment