Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to store temporary attributes on an Active Record model

I would like to store some debug messages on an object as it can go through processing but I don't want to be storing this to the database. Is something like this a reasonable way, an instance variable wrapped in a method with a nil guard? Or is there a better way / pattern?

class Bid < ApplicationRecord
  ...
  def debug_reasons
     @debug_reasons ||= []
  end
  ...

and then

bid.debug_reasons << "here is a reason"
like image 746
timpone Avatar asked Jan 28 '23 22:01

timpone


1 Answers

What you're looking for is attribute

class Bid < ApplicationRecord
  attribute :debug_reasons, :default => []
end

More info on attribute http://api.rubyonrails.org/v5.0/classes/ActiveRecord/Attributes/ClassMethods.html#method-i-attribute

like image 105
Josh Brody Avatar answered Jan 31 '23 23:01

Josh Brody