dan*_*ain 7 activerecord ruby-on-rails callback has-many-through
有没有办法动态添加after_add和after_remove回调现有的has_many或has_and_belongs_to_many关系?
例如,假设我有模型User,Thing和连接模型UserThingRelationship,User模型是这样的:
class User < ActiveRecord::Base
has_many :user_thing_relationships
has_many :things, :through => :user_thing_relationships
end
Run Code Online (Sandbox Code Playgroud)
我希望能够在一个模块中扩展User,添加:after_add和:after_remove回调User.has_many(:things, ...)关系.即,有类似的东西
module DoesAwesomeStuff
def does_awesome_stuff relationship, callback
# or however this can be achieved...
after_add(relationship) callback
after_remove(relationship) callback
end
end
Run Code Online (Sandbox Code Playgroud)
以便
class User < ActiveRecord::Base
has_many :user_thing_relationships
has_many :things, :through => :user_thing_relationships
does_awesome_stuff :things, :my_callback
def my_callback; puts "awesome"; end
end
Run Code Online (Sandbox Code Playgroud)
实际上是一样的
class User < ActiveRecord::Base
has_many :user_thing_relationships
has_many :things, :through => :user_thing_relationships, :after_add => :my_callback, :after_remove => :my_callback
def my_callback; puts "awesome"; end
end
Run Code Online (Sandbox Code Playgroud)
这可以非常有效地添加after_save等等,回调到正在扩展的模型,因为ActiveRecord::Base#after_save它只是一个类方法.
gle*_*ebm 12
最简单的是
User.after_add_for_things << lambda do |user, thing|
Rails.logger.info "#{thing} added to #{user}"
end
Run Code Online (Sandbox Code Playgroud)
我能够通过使用ActiveRecord :: Reflection得到以下内容:
module AfterAdd
def after_add rel, callback
a = reflect_on_association(rel)
send(a.macro, rel, a.options.merge(:after_add => callback))
end
end
class User < ActiveRecord::Base
extend AfterAdd
has_many :user_thing_relationships
has_many :things, :through => :user_thing_relationships
after_add :things, :my_callback
def my_callback
puts "Hello"
end
end
Run Code Online (Sandbox Code Playgroud)
我不想回答我自己的问题,所以如果其他人能够在接下来的几天内提出更好的解决方案,我就不会给自己回答.