sábado, 18 de março de 2017

RAILS Using console

1. Introduction

This post shows how to use RAILS Console.



2. Step-by-Step

2.1. Pre-Conditions

You should have a model post title:string body:text and another model comment post: reference body: text. References #1 has a step-by-step of creating objects


2.2. Executing some RAILS console commands


  • Run Console

$ rails console
Running via Spring preloader in process 2669
Loading development environment (Rails 5.0.2)


  • Get first record from model 'Post'

irb(main):001:0> Post.first
  Post Load (3.6ms)  SELECT  "posts".* FROM "posts" ORDER BY "posts"."id" ASC LIMIT $1  [["LIMIT", 1]]
=> #<Post id: 1, title: "1", body: "first", created_at: "2017-03-18 15:15:30", updated_at: "2017-03-18 15:16:05">


  • Get all records from model 'Post'

irb(main):004:0> Post.all
  Post Load (0.6ms)  SELECT "posts".* FROM "posts"
=> #<ActiveRecord::Relation [#<Post id: 1, title: "1", body: "first", created_at: "2017-03-18 15:15:30", updated_at: "2017-03-18 15:16:05">]>


  • Get records using criteria of filter created_at - result using JSON

irb(main):096:0* Post.where(created_at: Date.yesterday..Date.tomorrow)
  Post Load (0.3ms)  SELECT "posts".* FROM "posts" WHERE ("posts"."created_at" BETWEEN $1 AND $2)  [["created_at", Fri, 17 Mar 2017], ["created_at", Sun, 19 Mar 2017]]
=> #<ActiveRecord::Relation [#<Post id: 1, title: "1", body: "first", created_at: "2017-03-18 15:15:30", updated_at: "2017-03-18 15:16:05">]>

  • Get records using criteria of filter created_at - result using SQL
irb(main):108:0> Post.where(created_at: Date.yesterday..Date.tomorrow).to_sql
=> "SELECT \"posts\".* FROM \"posts\" WHERE (\"posts\".\"created_at\" BETWEEN '2017-03-17' AND '2017-03-19')"

  • Create a record of Post
irb(main):124:0* Post.create! title: 'Hello World', body: 'One World', created_at: '2017-03-15 15:45', updated_at: '2017-03-15 15:45'
   (0.1ms)  BEGIN
  SQL (47.5ms)  INSERT INTO "posts" ("title", "body", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["title", "Hello World"], ["body", "One World"], ["created_at", 2017-03-15 15:45:00 UTC], ["updated_at", 2017-03-15 15:45:00 UTC]]
   (22.7ms)  COMMIT
=> #<Post id: 2, title: "Hello World", body: "One World", created_at: "2017-03-15 15:45:00", updated_at: "2017-03-15 15:45:00">

  • Create a record of  'Comments' from first 'Post'
irb(main):001:0> Post.first.comments
Post Load (0.4ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."id" ASC LIMIT $1 [["LIMIT", 1]]
Comment Load (0.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 [["post_id", 1]]
=> #<ActiveRecord::Associations::CollectionProxy []>

  • Add a record of  'Comments' into first 'Post'
irb(main):002:0> Post.first.comments.create! body: 'Say something funny'
  Post Load (0.4ms)  SELECT  "posts".* FROM "posts" ORDER BY "posts"."id" ASC LIMIT $1  [["LIMIT", 1]]
   (0.1ms)  BEGIN
  SQL (1.0ms)  INSERT INTO "comments" ("post_id", "body", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["post_id", 1], ["body", "Say something funny"], ["created_at", 2017-03-18 19:19:38 UTC], ["updated_at", 2017-03-18 19:19:38 UTC]]
   (15.0ms)  COMMIT
=> #<Comment id: 1, post_id: 1, body: "Say something funny", created_at: "2017-03-18 19:19:38", updated_at: "2017-03-18 19:19:38">

  • Get records of  'Comments' from first 'Post' again - after 'Say something funny' created
irb(main):003:0> Post.first.comments
  Post Load (0.3ms)  SELECT  "posts".* FROM "posts" ORDER BY "posts"."id" ASC LIMIT $1  [["LIMIT", 1]]
  Comment Load (0.2ms)  SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1  [["post_id", 1]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Comment id: 1, post_id: 1, body: "Say something funny", created_at: "2017-03-18 19:19:38", updated_at: "2017-03-18 19:19:38">]>
irb(main):004:0>


3. References

Nenhum comentário:

Postar um comentário