From 19c6a59bb7edc518970e6edfe8c23078cc7fa2bb Mon Sep 17 00:00:00 2001 From: Logesh Mohanasundaram Date: Wed, 26 Oct 2016 12:22:01 +0530 Subject: [PATCH 1/3] Remove nil from hash --- rails_tip/2016-11-26-Retrieving ids copy.md | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 rails_tip/2016-11-26-Retrieving ids copy.md diff --git a/rails_tip/2016-11-26-Retrieving ids copy.md b/rails_tip/2016-11-26-Retrieving ids copy.md new file mode 100644 index 0000000..f148054 --- /dev/null +++ b/rails_tip/2016-11-26-Retrieving ids copy.md @@ -0,0 +1,25 @@ +--- +title: Remove nil from hash using Hash#compact and Hash#compact! +tip-number: 30 +tip-username: Logesh +tip-username-profile: https://github.com/logeshmallow +tip-description: We always had an issue when we send hash with nil value and now here is the option in ruby 2.4 to remove the nil using Hash#compact and Hash#compact! + +--- + + + + +We all would have faced removing nil value from array and hash and we had compact for removing nil from array and now in ruby 2.4, we also have option to remove it from hash + +```ruby + + hash = { "username" => "logesh", "company" => nil} + hash.compact #=> { "username" => "logesh" } + hash #=> { "username" => "logesh", "company" => nil} + + hash.compact! #=> { "username" => "logesh" } + hash #=> { "username" => "logesh" } +``` + +Since this comes with ruby itself, we even don't need rails to use this. From 9469185c17ae11870c05d89c579925b91cd6ebf8 Mon Sep 17 00:00:00 2001 From: Logesh Mohanasundaram Date: Mon, 31 Oct 2016 09:57:05 +0530 Subject: [PATCH 2/3] Add tip link to readme --- README.md | 2 ++ ...ng ids.md => 2016-03-29-Retrieving_ids_from_relationship.md} | 0 ...etrieving ids copy.md => 2016-11-26-Remove_nil_from_hash.md} | 0 3 files changed, 2 insertions(+) rename rails_tip/{2016-03-29-Retrieving ids.md => 2016-03-29-Retrieving_ids_from_relationship.md} (100%) rename rails_tip/{2016-11-26-Retrieving ids copy.md => 2016-11-26-Remove_nil_from_hash.md} (100%) diff --git a/README.md b/README.md index d26f355..a503f0d 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,8 @@ Being impressed with the awesomeness of jstip repository, I came up with the ide Please feel free to send us a pull request with your Rails tip to be published here. Any improvements or suggestions are more than welcome! # Tips list +- 30 - [Remove nil from hash in rails 5](https://github.com/logeshmallow/rails_tips/blob/master/rails_tip/2016-11-26-Remove_nil_from_hash.md) +- 29 - [Retrieving the ids of the has_many or has_many_through relationships](https://github.com/logeshmallow/rails_tips/blob/master/rails_tip/2016-03-29-Retrieving_ids_from_relationship.md) - 28 - [Support for left outer join in rails 5](https://github.com/logeshmallow/rails_tips/blob/master/rails_tip/2016-03-10-left_outer_join_in_Rails_5.md) - 27 - [Render partial from cache faster](https://github.com/logeshmallow/rails_tips/blob/master/rails_tip/2016-03-09-rendering_partial_from_cache_faster.md) - 26 - [Increase productivity with console tricks](https://github.com/logeshmallow/rails_tips/blob/master/rails_tip/2016-03-08-Increase_productivity_with_few_tricks.md) diff --git a/rails_tip/2016-03-29-Retrieving ids.md b/rails_tip/2016-03-29-Retrieving_ids_from_relationship.md similarity index 100% rename from rails_tip/2016-03-29-Retrieving ids.md rename to rails_tip/2016-03-29-Retrieving_ids_from_relationship.md diff --git a/rails_tip/2016-11-26-Retrieving ids copy.md b/rails_tip/2016-11-26-Remove_nil_from_hash.md similarity index 100% rename from rails_tip/2016-11-26-Retrieving ids copy.md rename to rails_tip/2016-11-26-Remove_nil_from_hash.md From 17e68b3cb2f9408f8ae382fa842dd12335f0cbcf Mon Sep 17 00:00:00 2001 From: Logesh Mohanasundaram Date: Mon, 31 Oct 2016 10:31:08 +0530 Subject: [PATCH 3/3] tip - Remove ambiguous column name issue in rails5 --- README.md | 1 + ...olumn_in_select_and_group_fix_in_rails5.md | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 rails_tip/2016-11-31-Ambiguous_column_in_select_and_group_fix_in_rails5.md diff --git a/README.md b/README.md index a503f0d..c317d3d 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ Being impressed with the awesomeness of jstip repository, I came up with the ide Please feel free to send us a pull request with your Rails tip to be published here. Any improvements or suggestions are more than welcome! # Tips list +- 31 - [Remove ambiguous column issue in rails 5](https://github.com/logeshmallow/rails_tips/blob/master/rails_tip/2016-11-31-Ambiguous_column_in_select_and_group_fix_in_rails5.md) - 30 - [Remove nil from hash in rails 5](https://github.com/logeshmallow/rails_tips/blob/master/rails_tip/2016-11-26-Remove_nil_from_hash.md) - 29 - [Retrieving the ids of the has_many or has_many_through relationships](https://github.com/logeshmallow/rails_tips/blob/master/rails_tip/2016-03-29-Retrieving_ids_from_relationship.md) - 28 - [Support for left outer join in rails 5](https://github.com/logeshmallow/rails_tips/blob/master/rails_tip/2016-03-10-left_outer_join_in_Rails_5.md) diff --git a/rails_tip/2016-11-31-Ambiguous_column_in_select_and_group_fix_in_rails5.md b/rails_tip/2016-11-31-Ambiguous_column_in_select_and_group_fix_in_rails5.md new file mode 100644 index 0000000..4278f75 --- /dev/null +++ b/rails_tip/2016-11-31-Ambiguous_column_in_select_and_group_fix_in_rails5.md @@ -0,0 +1,32 @@ +--- +title: Removing ambiguous column issue in rails 5 +tip-number: 31 +tip-username: Logesh +tip-username-profile: https://github.com/logeshmallow +tip-description: Whenever we have columns with same name in two table and query it, we face ambiguous column issue in select and group by and rails5 fixes it now + +--- + + +```ruby + + users(:id, :name) + posts(:id, :title, :user_id) + comments(:id, :description, :user_id, :post_id) + + >> Post.joins(:comments).group(:user_id).count + Mysql2::Error: Column 'user_id' in field list is ambiguous: SELECT COUNT(*) AS count_all, user_id AS user_id FROM `posts` INNER JOIN `comments` ON `comments`.`post_id` = `posts`.`id` GROUP BY user_id + + + users(:id, :name) + posts(:id, :title, :user_id) + comments(:id, :description, :user_id, :post_id) + + >> Post.joins(:comments).group(:user_id).count + SELECT COUNT(*) AS count_all, "posts"."user_id" AS posts_user_id FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" GROUP BY "posts"."user_id" + + => { 1 => 1 } + +``` + +This shows that now both projection and Group By are prepended with the posts table name and hence fixing the conflict.