0

I have used draper with some success. But currently I'm stuck.

I have two models:

# Foo.rb
class Foo < ActiveRecord::Base
    has_many :bars


# Bar.rb
class Bar < ActiveRecord::Base
    belongs_to :foo

    def self.number_banned
      where(:status => "banned").count
    end

If the class is loaded normally, it works as expected:

f = Foo.find(1)
f.bars.number_banned  # ex. 3

If I use Draper to decorate the objects, it returns an undefined "number_banned" method:

f = Foo.find(1).decorate
f.bars.number_banned  # Undefined method 'number_banned'

To note:

  • Both Decorators include the delegate_all option
  • FooDecorator decorates_association :bars

Any Ideas what I'm doing wrong?

HermannHH
  • 1,732
  • 1
  • 27
  • 57

1 Answers1

0

Thank you for the comments. I seem to have found a solution.

The FooDecorates method should point to a singular instance of Bar:

 decorates_association :bar

And not:

decorates_association :bars
HermannHH
  • 1,732
  • 1
  • 27
  • 57