Ohm::IndexNotFound: Ohm::IndexNotFound on adding to a collection or a set

176 views
Skip to first unread message

mattcr...@mac.com

unread,
Mar 11, 2014, 10:49:28 AM3/11/14
to ohm-...@googlegroups.com
I have a simple model to hold a set of hierarchical tags

class BrowserTerm < Ohm::Model
attribute :label
index :label
attribute :display_name
set :parents, :BrowserTerm
set :children, :BrowserTerm
end

When I go to use these, however, I end up with an Ohm::IndexNotFound: Ohm::IndexNotFound exception.

1.9.3-p362 :008 > bt=BrowserTerm.create(label: "ROOT_TERM", display_name: "Browser Root")
 => #<BrowserTerm:0x007fc4839767e8 @attributes={:label=>"ROOT_TERM", :display_name=>"Browser Root"}, @_memo={}, @errors={}, @id="10"> 
1.9.3-p362 :010 > bt.children.add(label: "CHILD1", display_name: "First Child")
Ohm::IndexNotFound: Ohm::IndexNotFound
from /Users/Matt1/.rvm/gems/ruby-1.9.3-p362/gems/ohm-1.2.0/lib/ohm.rb:1418:in `toindices'
from /Users/Matt1/.rvm/gems/ruby-1.9.3-p362/gems/ohm-1.2.0/lib/ohm.rb:1414:in `block in filters'
from /Users/Matt1/.rvm/gems/ruby-1.9.3-p362/gems/ohm-1.2.0/lib/ohm.rb:1414:in `each'
from /Users/Matt1/.rvm/gems/ruby-1.9.3-p362/gems/ohm-1.2.0/lib/ohm.rb:1414:in `map'
from /Users/Matt1/.rvm/gems/ruby-1.9.3-p362/gems/ohm-1.2.0/lib/ohm.rb:1414:in `filters'
from /Users/Matt1/.rvm/gems/ruby-1.9.3-p362/gems/ohm-1.2.0/lib/ohm.rb:826:in `find'
from /Users/Matt1/.rvm/gems/ruby-1.9.3-p362/gems/ohm-1.2.0/lib/ohm.rb:938:in `block in collection'
from (irb):10
from /Users/Matt1/.rvm/gems/ruby-1.9.3-p362/gems/railties-3.2.6/lib/rails/commands/console.rb:47:in `start'
from /Users/Matt1/.rvm/gems/ruby-1.9.3-p362/gems/railties-3.2.6/lib/rails/commands/console.rb:8:in `start'
from /Users/Matt1/.rvm/gems/ruby-1.9.3-p362/gems/railties-3.2.6/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

I tried using Jruby and vanilla ruby (1.9.3-p362).
Using ohm (1.2.0) 
Using redis (3.0.2) 


Any ideas?

Michel Martens

unread,
Mar 11, 2014, 10:56:14 AM3/11/14
to ohm-...@googlegroups.com
Yes, the problem is that bt.children.add(...) expects a model
instance, so you can rewrite it like this:

bt.children.add(BrowserTerm.create(label: "CHILD1", display_name:
"First Child"))

If you prefer, you can create a helper method:

class BrowserTerm < Ohm::Model
...

def add_child(dict)
children.add(self.class.create(dict))
end
end

mattcr...@mac.com

unread,
Mar 11, 2014, 3:23:22 PM3/11/14
to ohm-...@googlegroups.com
Thanks!  That seems to work fine… until I go to fetch the children.  For some reason, there seems to be an issue getting them back:
class BrowserTerm < Ohm::Model
attribute :label
index :label
attribute :display_name
list :parents, :BrowserTerm
list :children, :BrowserTerm
def add_child(dict) 
    children.add(self.class.create(dict)) 
  end
    def add_parent(dict) 
    parents.add(self.class.create(dict)) 
  end
end


root=BrowserTerm.create(label: "ROOT", display_name: "ROOT")
=> #<BrowserTerm:4 label="ROOT" display_name="ROOT" parents=#<List (BrowserTerm): []> children=#<List (BrowserTerm): []>>
root.children.add(BrowserTerm.create(label: "child", display_name: "child"))
=> 1
root.children
=> #<List (BrowserTerm): ["5"]>
root.children.first
=> nil

Michel Martens

unread,
Mar 11, 2014, 4:38:48 PM3/11/14
to ohm-...@googlegroups.com
On 11 March 2014 16:23, <mattcr...@mac.com> wrote:
> add


Try root.children.push(...) instead.

mattcr...@mac.com

unread,
Mar 11, 2014, 5:22:44 PM3/11/14
to ohm-...@googlegroups.com
NoMethodError: undefined method `push' for #<List (BrowserTerm): []>

Michel Martens

unread,
Mar 11, 2014, 5:26:24 PM3/11/14
to ohm-...@googlegroups.com
Which version are you using?
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "Ohm Ruby" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ohm-ruby+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

mattcr...@mac.com

unread,
Mar 11, 2014, 11:39:58 PM3/11/14
to ohm-...@googlegroups.com

Michel Martens

unread,
Mar 12, 2014, 10:22:22 AM3/12/14
to ohm-...@googlegroups.com
Can you double check? Or maybe take a look at the code of the installed gem.

The reason for that is because List#push is defined in 1.2.0, so the
error you are
gettin is very strange. See this:

https://github.com/soveran/ohm/blob/1.2.0/lib/ohm.rb#L246-L249
Reply all
Reply to author
Forward
0 new messages