How to save current model in my method

I have a Rails model and in one of the methods I create pdf using such a shrimp,

class Report < ActiveRecord::Base def pdf_output Prawn::Document.new do text "Start date: #{start_date.strftime('%e %b %Y').squish}" end end end 

In this text method, I am trying to infer the start_date attribute of my report model. Instead, I get the following error:

 NoMethodError in ReportsController#show undefined method `start_date' for #<Prawn::Document:0x007fdafbce6930> 

So my start_date method refers to my Document object instead of the Report object. How to access the variables and methods of my report object from this block?

+4
source share
1 answer

A normal JavaScript trick should work:

 def pdf_output report = self Prawn::Document.new do text "Start date: #{report.start_date.strftime('%e %b %Y').squish}" end end 

Just grab the self link you need so you don't have to worry about what the shrimp does with self inside your block.

+5
source

Source: https://habr.com/ru/post/1439738/


All Articles