Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

has_many finds deep elements, not just immediate children #60

Closed
dmolesUC opened this issue Apr 30, 2015 · 2 comments
Closed

has_many finds deep elements, not just immediate children #60

dmolesUC opened this issue Apr 30, 2015 · 2 comments

Comments

@dmolesUC
Copy link

Given XML like the following:

  <qux>
    <foo id="0"/>
    <foo id="1"/>
    <baz>
      <foo id="2"/>
      <foo id="3"/>
    </baz>
  </qux>

Mapping this with happymapper, Qux.foo should be an array of size 2 containing only the <foo>s with IDs 0 and 1, but in fact it's an array of size 4 containing both those and the deeply nested <foo>s with IDs 2 and 3, as demonstrated below:

#! /usr/bin/env ruby

require 'happymapper'

class Foo
  include HappyMapper

  tag 'foo'

  attribute :id, Integer
end

class Baz
  include HappyMapper

  tag 'baz'

  has_many :foo, Foo
end

class Qux
  include HappyMapper

  tag 'qux'

  has_many :foo, Foo
  element :baz, Baz
end

xml = '
  <qux>
    <foo id="0"/>
    <foo id="1"/>
    <baz>
      <foo id="2"/>
      <foo id="3"/>
    </baz>
  </qux>
'

qux = Qux.parse(xml)

puts 'qux/foo'
qux.foo.each { |f| puts f.id }

puts 'qux/baz/foo'
qux.baz.foo.each { |f| puts f.id }

This should print

qux/foo
0
1
qux/baz/foo
2
3

but instead it prints

qux/foo
0
1
2
3
qux/baz/foo
2
3
@FlorianFranzen
Copy link

The default xpath is .// a.k.a. a greedy one.

Use has_many :foo, Foo, xpath: '.' or better has_many :foo, Foo, deep: false to get what you want.

@dmolesUC
Copy link
Author

dmolesUC commented Aug 6, 2015

You're right, that's explicitly covered in the readme. Apologies.

@dmolesUC dmolesUC closed this as completed Aug 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants