问题
I have an audit monkey patch to ActiveRecord that works on all versions of Rails from 2.x to 4.0.2 but does not work with ActiveRecord 4.1. The code for 4.0 looks like this
module HLLAuditStamps
def self.included(base)
# create/update became create_record/update_record in Rails-4.0
base.alias_method_chain :create_record, :audit
base.alias_method_chain :update_record, :audit
private
def create_record_with_audit
set_audit_attributes
. . .
This works in 4.0.2 but throws this exception in 4.1.2:
undefined method create_record' for class
ActiveRecord::Base' (NameError)
If I go into rails console in a rails-4.1.2 project and list the methods in ActivRecord::Base I see this:
. . .
- :count_by_sql
- :create
- :create!
- :create_with
- :current_scope
. . .
As the 'create' method is what was in vogue before 4.0 that was what the original audit patch used. I returned to the original from and removed the '_record' segment throughout the module. However, that simply throws this exception:
undefined method `create' for class `ActiveRecord::Base' (NameError)
So, where did create/create_record go to in ActiveRecord 4.1 and why am I unable to reference a method that is evidently part of the that class ?
edit: Well, it went here:
#rails / activerecord / lib / active_record.rb
module ActiveRecord
extend ActiveSupport::Autoload
. . .
autoload :Persistence
. . .
So, now I just have to figure out what to do with the information.
回答1:
See: Does adding a :before_save to an AR model override one already added through a custom extension to AR?
example:
module HLLAuditStamps
extend ActiveSupport::Concern
included do
before_save :set_audit_attributes
end
private
def set_audit_attributes
. . .
end
end
class ActiveRecord::Base
include HLLAuditStamps
end
来源:https://stackoverflow.com/questions/24962480/where-has-create-create-record-gone-in-activerecord