Created
September 5, 2019 15:01
-
-
Save staycreativedesign/0ebddec45c9f178d2750d88daa1b1d7d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@count = @entries.map(&:type).each_with_object(Hash.new(0)) { |type, hash| hash[type] += 1 } | |
=> | |
{ | |
#<Type:0x00007fd3a9a4f810 id: 4, title: "Events", created_at: Mon, 19 Aug 2019 18:22:02 UTC +00:00, updated_at: Mon, 19 Aug 2019 18:22:02 UTC +00:00, slug: "events">=>2, | |
#<Type:0x00007fd3a38f6cd0 id: 10, title: "Topic", created_at: Mon, 19 Aug 2019 18:22:02 UTC +00:00, updated_at: Mon, 19 Aug 2019 18:22:02 UTC +00:00, slug: "topic">=>1, | |
#<Type:0x00007fd3a7b2f4e0 id: 5, title: "Restaurants", created_at: Mon, 19 Aug 2019 18:22:02 UTC +00:00, updated_at: Mon, 19 Aug 2019 18:22:02 UTC +00:00, slug: "restaurants">=>2, | |
} | |
How can I remove any Type that has title of Topic ? |
nilbus
commented
Sep 5, 2019
•
Technically you could do it this way ☝️, but if @entries
is an ActiveRecord::Relation, then I'd recommend doing it at the model layer.
class Entry < ApplicationModel
# surely there's a better name in your domain for things that
# aren't "Topic"—the reason you're excluding it.
scope :not_topic, -> { where.not(title: 'Topic') }
end
count = @entries.not_topic.each_with_object(Hash.new(0)) { |type, hash| hash[type] += 1 }
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment