Search code examples
ruby-on-railsrubyactiverecordprawnto

can't access ActiveRecord mutators in a block


I am inside a Rails controller and I am trying to access my instance variable in a block: This gives an error saying that "no method field1 for Nil":

Prawn::Document.generate("hello.pdf") do
  @model.field1
end

However, if I do this, then it works:

my_model = @model
Prawn::Document.generate("hello.pdf") do
  my_model.field1
end

Could this have something to do with ActiveRecord accessors or instance variables in a block?


Solution

  • That's happening because code inside block is executed in context of Prawn::Document object. Let's go inside this code:

    module Prawn
      class Document
        def self.generate(filename,options={},&block)
          pdf = new(options,&block)
          pdf.render_file(filename)
        end
    
        def initialize(options={},&block)
          if block
            block.arity < 1 ? instance_eval(&block) : block[self]
          end
        end
      end
    end
    

    As you can see, block is executed with Document object as self. It try to find @model as instance variable of self, can't do this and return nil. If you use local variable model, you get help of closures and your code is working properly