Home > Software engineering >  Rails ignore coder in model
Rails ignore coder in model

Time:12-30

I need to run a rake task to migrate data in my model

class Translation < ApplicationRecord
  store :body, accessors: [:object_body], coder: YAML
  belongs_to :team
end

and need to access body as text. I don't want to remove the coder part since a lot of logic relies on that. Can I access body without decoding to a ruby object?

CodePudding user response:

Just add a new method on the model.

class Translation < ApplicationRecord
  store :body, accessors: [:object_body], coder: YAML
  belongs_to :team

  def raw_body
    read_attribute_before_type_cast('body')
  end
end

CodePudding user response:

read_attribute_before_type_cast should work. It will return the value before typecasting or deserialization.

raw_body = obj.read_attribute_before_type_cast(:body)
  • Related