Question

I have installed ancestry gem, in gemfile:

gem "ancestry", "~> 2.0.0"

In model:

class Category < ActiveRecord::Base
  attr_accessible :..., :name, :..., :ancestry, :..., :...
  #acts_as_tree
  has_ancestry cache_depth: true
  has_many :cars
end

And when I load in a view the respective category:

- category = Category.where('my_category_id = ?', @game.car_category_id)
%div= category.inspect

The output is

[#<Category id: 58, name: "Name", ..., created_at: "2013-11-22 15:00:01", updated_at: "2013-11-24 14:54:06", ancestry: "7", car_category_id: 6205>]

So in the column ancestry is the value 7. When I try to display this value, = category.ancestry, the result is:

undefined method `ancestry' for #<ActiveRecord::Relation:0x007fce5e61dcb0>

The same also when I try to inspect that value...

What am I doing wrong here? Why I cannot work with ancestry value?

Thank you

Was it helpful?

Solution

get a real instance, so replace:

category = Category.where('my_category_id = ?', @game.car_category_id)

with:

category = Category.where('my_category_id = ?', @game.car_category_id).first

Btw, this should be done in your controller. Not in your view. ( and using scopes wont hurt :) )

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top