Problems implementing ActiveModel Dirty Rails 3.2.8

I want to check if the attributes on the model have changed. I tried checking the values! = The value in the form before doing the save, but this code is really ugly and does not work well from time to time. Same thing with using update_column, which does not perform validation in my model class. If I use update_attributes without doing anything else, I cannot check when the field was updated from my understanding. From my web research on Qaru and other sites, it seems like using ActiveModel Dirty is the way to go.

I looked at this: http://api.rubyonrails.org/classes/ActiveModel/Dirty.html

I hope to use this to check if the logical flags on the model have changed after using update_attributes. I tried to perform a minimal implementation as described in the included link. I added the following to the ActiveRecord class:

include ActiveModel::Dirty

define_attribute_methods [:admin]

I tried adding three attributes that I wanted to track. I started with one attribute to see if I can make it work. When running the rspec test, I received the following error. As soon as I deleted the argument, I had no errors.

Exception encountered: #<ArgumentError: wrong number of arguments (1 for 0)>

After I removed the argument, I decided to include similar methods in my model, using admin instead of a name. Other Rspec tests have violated the save method. However, I feel the problem is how I implement ActiveModel Dirty.

, , 3.2.8, 3.2.6 3.2.8. , , ActiveModel:: Dirty admin_changed? , .

- , , , . , , , Rails .

, . , , . , - .

+4
2

, ActiveRecord define_attribute_methods 0 ( ActiveRecord ): https://github.com/rails/rails/blob/master/activerecord/lib/active_record/attribute_methods.rb#L23

define_attribute_methods, ActiveModel: https://github.com/rails/rails/blob/master/activemodel/lib/active_model/attribute_methods.rb#L240

:

, ...

lib/active_record/nonpersisted_attribute_methods.rb: https://gist.github.com/4600209

- :

require 'active_record/nonpersisted_attribute_methods'
class Foo < ActiveRecord::Base
  include ActiveRecord::NonPersistedAttributeMethods
  define_nonpersisted_attribute_methods [:bar]
end

foo = Foo.new
foo.bar = 3
foo.bar_changed? # => true
foo.bar_was # => nil
foo.bar_change # => [nil, 3]
foo.changes[:bar] # => [nil, 3]

, , , :

DEPRECATION WARNING: You're trying to create an attribute `bar'. Writing arbitrary attributes on a model is deprecated. Please just use `attr_writer` etc.

, , Rails 4...

. :

+8

add =, :

define_attribute_methods = [:admin]

. , - this?

-3

All Articles