I'm trying to pass in a User object which needs to be set in after_build, but setting a class variable in the initializer sets it on the CSVImporter class, and after_build seems to run in the context of CSVImporter::Row, meaning all attempts to access the instance variable return nil.
Is there a way to access the instance variable in after_build?
class MyImporter
include ::CSVImporter
model Ticket
…
after_build do |ticket|
ticket.created_by = @user
end
def initialize(user)
@user = user
end
end
class MyImporter
include ::CSVImporter
model Ticket
end
user = User.first!
MyImporter.new(...) do
after_build do |ticket|
ticket.created_by = user
end
end
I'm trying to pass in a User object which needs to be set in
after_build
, but setting a class variable in the initializer sets it on theCSVImporter
class, andafter_build
seems to run in the context ofCSVImporter::Row
, meaning all attempts to access the instance variable returnnil
.Is there a way to access the instance variable in
after_build
?