Skip to content

Instantly share code, notes, and snippets.

@burtlo
Last active December 27, 2015 00:19
Show Gist options
  • Save burtlo/7237359 to your computer and use it in GitHub Desktop.
Save burtlo/7237359 to your computer and use it in GitHub Desktop.
Nokogiri Happymapper - Assignment of Parent Element
require 'happymapper'
class Branch
include HappyMapper
attr_accessor :tree
end
class Tree
include HappyMapper
has_many :branches, Branch
def branches
@branches.each { |branch| branch.tree = self }
end
end
tree_xml = %{
<tree>
<branch name="1"></branch>
<branch name="2"></branch>
<branch name="3"></branch>
</tree>
}
tree = Tree.parse(tree_xml,single: true)
puts tree.branches
puts tree.branches.first.tree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment