RubyMoney/money-rails

write_attribute private method does not exist anymore in ActiveModel 6.1

berkos opened this issue ยท 7 comments

Context

money-rails documentation refers only to to ActiveRecord although since Rails 6.0.3.4 money-rails gem used to work for both ActiveModel and ActiveRecord Rails classes.

money-rails is using a private method write_attribute that was removed on Rails 6.1
Relevant commit

Steps to reproduce

Use monetize class method in an ActiveModel class.

code sample:

class Foo
  def self.after_save
    # NOTE: to get around: https://github.com/RubyMoney/money-rails/issues/566
  end

  include ActiveModel::AttributeMethods
  include ActiveModel::Validations
  include MoneyRails::ActiveRecord::Monetizable

  def initialize(price_subunit:)
    @price_subunit = price_subunit
  end

  attr_accessor :price_subunit

  monetize :price_subunit
end

foo = Foo.new(price_subunit: 1234)
foo.price = 20.00

Expected behavior

The above should not raise an error

Actual behavior

=> #<Foo:0x00007fa80f7db270 @price_subunit=1234>
Traceback (most recent call last):
        1: from (irb):20
NoMethodError (undefined method `write_attribute' for #<Foo:0x00007fa80f7db270>)

System

Rails version: 6.1
Ruby version: 2.7.2

Please let me know if there any more information I should provide.
I thought it was better to report this early in case somone might be experiencing something similar.

@berkos thanks for noting, this is very interesting. I assume we can just call super in the methods that override accessors. I'll give that a go. Also thanks for linking the Rails PR, super helpful context ๐Ÿ‘

@antstorm || @berkos care to submit a PR?

Would be good to also bypass that after_save call if the object doesn't respond to it

fzero commented

@berkos thanks for noting, this is very interesting. I assume we can just call super in the methods that override accessors. I'll give that a go. Also thanks for linking the Rails PR, super helpful context ๐Ÿ‘

Hi there! I bumped into this issue just now and I'd like to know if there's an active PR for it. I'd be happy to contribute a fix, but I don't want to duplicate the effort. Any pointers/pitfalls to look for are welcome!

none that I am aware of @fzero

Hi @fzero, thanks for your message and for looking into it! ๐Ÿ™

Not any work started that I am aware of too. I can't remember any specific pointers but looking at the description once again I hope it should be sufficient to reproduce the error.

is there a reason this is closed and marked as completed? it's still broken for me on rails 7.1. (same with #566)