Search code examples
ruby-on-railsactiverecordancestry

Access object wrapped in curly braces


I have this object that is returned from from a query using some Ancestry Gem methods:

 @category = ItemsCategory.find(params[:id_and_name]).subtree.arrange

This is the result:

category = {#<ItemsCategory id: 16, name: "Coffee Tables", created_at: "2014-04-02 19:50:26", updated_at: "2014-06-03 21:03:03", title_tag: nil, description_tag: nil, ancestry: "144/1/7">=>{}}

How can I get to the different keys? If I say @category.id I get @category has no method id


Solution

  • Your result is a hash, with the key being the ItemsCategory object you are trying to access. Notice the =>{}} at the end of the line. This is because your ItemsCategory has no child instances associated with it.

    If you still want to access this, do as so:

    @category.keys.first.id # => 16