alias_method_chain有非常强大的功能,使得我们对Rails的功能扩展变得很容易。考虑过当一个方法存在多个alias_method_chain时,without方法会变成什么样子呢?但一直没有做相关的调查。
在进行Rails2.2.2到2.3.8的升级过程中,终于遭遇了这个问题。
ActiveRecord::Base的update方法,有四个alias_method_chain:
base.alias_method_chain :update, :callbacks
base.alias_method_chain :update, :timestamps
base.alias_method_chain :update, :dirty
base.alias_method_chain :update, :lock
由于我们的程序会在后台处理客户的信息,而不像改变updated_at,所以公有化了update_without_timestamps,显示的调用它。而在update_without_timestamps执行后,我们需要after_update来触发缓存的清空/更新。
Rails2.2.2里,method_chain实际工作顺序如下。update_without_timestamps执行后,正确的执行了callbacks来清空缓存,我们的程序工作正常。
引用
update_with_timestamps # update_without_timestamps = update_with_callbacks
update_with_callbacks
update_with_dirty
update_with_lock
但是在2.3.8下,method_chain工作顺序变化了。执行update_without_timestamps的时候,直接跳过了update_with_callbacks,程序出错。
引用
update_with_callbacks
update_with_timestamps # update_without_timestamps = update_with_dirty
update_with_dirty
update_with_lock
看来在享用alias_method_chain便利的同时,还要注意它的一些细节呀。
代码的修改其实很简单,只要利用ActiveRecord::Base.record_timestamps = false就好了。
分享到:
相关推荐
确保两个模型都继承自ActiveRecord :: Base class Firm < ActiveRecord :: Base #... end class Client < ActiveRecord :: Base #... end 添加您的has_many和belongs_to class Firm < ...
针对Rails 4应用程序的ActiveRecord::Relation#in_batches实验性ActiveRecord::Relation#in_batches移植。安装将此行添加到您的Gemfile中。 gem "activerecord-in_batches", git: "git://github....
ActiveRecord :: Base def self . slow_class_method end def slow_instance_method end end 支持类方法和ActiveRecord实例方法 MyObject . async ( :high ) . slow_class_method # ActiveRecord in
主动记录一种轻松使用ActiveRecord的...配置您的数据库连接加载了gem之后,请使用以下示例之一将有关数据库配置的信息告知OTR::ActiveRecord : OTR::ActiveRecord.configure_from_file! "config/database.yml"OTR::
在ActiveRecord可以用很方便的声明方式来定义model之间的关联关系,例如: 代码如下: class Topic < ActiveRecord::Base has_many :posts belongs_to :user end has_many和belongs_to其实是Topic类的class ...
红流 使用redis流使您的主数据库与辅助数据存储(例如elastic... ActiveRecord :: Base include Redstream :: Model # ... redstream_callbacks # ... end redstream_callbacks将after_save , after_touch , a
ActiveRecord简单实例_activerecord.zip
Castle.ActiveRecord For .NET FrameWork 2.0 ...Castle.ActiveRecord.dll:V2.1.2.6725 NHibernate.dll:V2.1.2.4000 Castle.Core.dll:V1.1.0.0 Castle.Components.Validator.dll:V1.0.3.4333
ActiveRecord :: Base include IdentityCache include IdentityCacheFetchers cache_index :handle , unique : true cache_index :vendor , :product_typeend 这个 gem 为您带来了等价物,但使用 IdentityCache ...
ActiveRecord :: Base has_many :comments end class Comment < ActiveRecord :: Base belongs_to :article end 然后,您通常需要将Comment内容反规范化为Post ,反之亦然。 这是在帖子上反规范last_comment_...
关系注解使用自定义数据注释 ActiveRecord 关系对象,... ActiveRecord :: base include ActiveRecord :: RelationsAnnotationsendusers = User . where . not ( email : nil )with_email_users = users . annotate
Sortiri是一种干净轻巧的解决方案,用于使ActiveRecord :: Base对象可排序。 描述Sortiri是一种干净轻巧的解决方案,用于使ActiveRecord :: Base对象可排序。 入门Ruby 2.6.0和更高版本上的ActiveRecord 4.2+支持...
ActiveRecord :: Base smart_json_style ( :simple ) { { title : title } } smart_json_style ( :with_detail , :simple , author : :only_name , comments : :with_user ) end class User < ActiveRecord ::...
class Order < ActiveRecord::Base state :unpaid, :allow => [:latest, :pay, :cancel] state :cancelled, :allow => :latest transition :latest, {:action => :show} transition :cancel, {:action => :destroy},...
Activerecord :: Mysql ::未签名 为ActiveRecordMySQL2适配器将无符号选项添加到整数类型。 支持版本 5.0 > ActiveRecord::VERSION >= 3.2 安装 将此行添加到您的应用程序的Gemfile中: gem 'activerecord-mysql...
I18n ::后端:: ActiveRecord 该存储库包含I18n ActiveRecord后端和从提取的支持代码。 它与Rails 3、4、5和6完全兼容。 安装 对于Bundler,请将以下内容放入您的Gemfile中: gem 'i18n-active_record', :require =...
2、Castle.ActiveRecord.Web 3、Castle.Components.Validator 4、NHibernate.JetDriver 5、NHibernate.Search 为了和原有版本区分开,特地将文件版本均升级到了3.4.0.4000,产品版本没有改动。 关键词:Castle....
有令牌创建用于访问 ActiveRecord 对象的令牌。 允许您以声明方式为 ActiveRecord 对象指定基于... ActiveRecord::Base HasTokens.on self has_tokens public: 5 before_validation :generate_tokens, on: :create vali
多站点 MultiSite 提供按租户确定... ActiveRecord :: Base # ... end 在你的模型中,你需要插入类方法has_site_scope来声明哪些是由站点限定的: class User < ActiveRecord :: Base has_site_scope end c
ActiveRecord :: Base . establish_connection ( adapter : 'mysql2' , database : 'my_db' ) ActiveRecord :: Migration . create_table :geoms , options : 'ENGINE=MyISAM' do | t | t . geometry 'location' , ...