Skip to content

Instantly share code, notes, and snippets.

@hasanen
Last active August 29, 2015 14:13
Show Gist options
  • Save hasanen/69f0f1a06c74017d5a77 to your computer and use it in GitHub Desktop.
Save hasanen/69f0f1a06c74017d5a77 to your computer and use it in GitHub Desktop.
N+1 problem
class Industry
def initialize name, parent=nil
@name, @parent = name, parent
end
def full_name
[parent_name, @name].reject{|n| n.empty?}.join(' / ')
end
def parent_name
@parent.nil? ? '' : @parent.full_name
end
end
ind1 = Industry.new 'Name 1'
ind2 = Industry.new 'Name 2', ind1
ind3 = Industry.new 'Name 3', ind2
puts ind3.full_name # => "Name 1 / Name 2 / Name 3"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment