Skip to content

Instantly share code, notes, and snippets.

@rpdecks
Created April 7, 2020 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpdecks/26ce5dda83e6c6010df95b53fe31abe6 to your computer and use it in GitHub Desktop.
Save rpdecks/26ce5dda83e6c6010df95b53fe31abe6 to your computer and use it in GitHub Desktop.
class Ingredient
@@all = []
attr_reader :name
def initialize(name)
@name = name
@@all << self
end
def self.all
@@all
end
def users
## isn't this missing the select? I think you're trying to do Allergy.all.select then map?
allergens = Allergy.all do |allergy|
allergy.ingredient == self
end
allergens.map { |allergy| allergy.user }.uniq
end
def self.most_common_allergen
all.max_by do |ingredient|
ingredient.users.count
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment