Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stevehodges/752fcf68924670768398 to your computer and use it in GitHub Desktop.
Save stevehodges/752fcf68924670768398 to your computer and use it in GitHub Desktop.
Loading development environment (Rails 4.2.1)
irb(main):001:0> p = Post.first
Post Load (0.2ms) SELECT `posts`.* FROM `posts` ORDER BY `posts`.`id` ASC LIMIT 1
=> #<Post id: 1, title: "I love amoebas", approved_version_id: nil>
irb(main):002:0> p.comments
Comment Load (0.3ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`post_id` = 1
=> #<ActiveRecord::Associations::CollectionProxy [#<Comment id: 1, snarky_comment: "amoebas are too fuzzy", post_id: 1, approved_version_id: nil>]>
irb(main):003:0> p.comments.map(&:ratings)
Rating Load (0.4ms) SELECT `ratings`.* FROM `ratings` WHERE `ratings`.`comment_id` = 1
=> [#<ActiveRecord::Associations::CollectionProxy [#<Rating id: 1, rating: 2, comment_id: 1, approved_version_id: nil>]>]
irb(main):004:0> dupe = p.amoeba_dup
=> #<Post id: nil, title: "I love amoebas", approved_version_id: 1>
irb(main):005:0> dupe.save
(0.2ms) BEGIN
SQL (0.2ms) INSERT INTO `posts` (`title`, `approved_version_id`) VALUES ('I love amoebas', 1)
SQL (0.2ms) INSERT INTO `comments` (`snarky_comment`, `approved_version_id`, `post_id`) VALUES ('amoebas are too fuzzy', 1, 2)
SQL (0.1ms) INSERT INTO `ratings` (`rating`, `comment_id`) VALUES (2, 2)
(4.3ms) COMMIT
=> true
irb(main):006:0> dupe.approved_version
Post Load (0.4ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 1 LIMIT 1
=> #<Post id: 1, title: "I love amoebas", approved_version_id: nil>
irb(main):007:0> p.draft
Post Load (0.4ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`approved_version_id` = 1 LIMIT 1
=> #<Post id: 2, title: "I love amoebas", approved_version_id: 1>
irb(main):008:0> p.comments.first.draft
Comment Load (0.4ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`approved_version_id` = 1 LIMIT 1
=> #<Comment id: 2, snarky_comment: "amoebas are too fuzzy", post_id: 2, approved_version_id: 1>
irb(main):009:0> p.comments.first.draft.approved_version
Comment Load (0.4ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`id` = 1 LIMIT 1
=> #<Comment id: 1, snarky_comment: "amoebas are too fuzzy", post_id: 1, approved_version_id: nil>
irb(main):010:0> p.comments.first.ratings.map(&:approved_version_id)
=> [nil]
irb(main):011:0> p.comments.first.ratings.map(&:approved_version_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment