On Feb 10, 9:14 am, Jeremy Evans <[email protected]> wrote: > On Feb 9, 8:50 pm, John Firebaugh <[email protected]> wrote: > > > > > On Wed, Feb 9, 2011 at 6:33 PM, Jeremy Evans <[email protected]> wrote: > > > I was planning on adding the feature as a plugin before 3.21.0. It's > > > fairly simple. Use an anonymous module for each class, and for every > > > many_to_one call, add the foreign key setter method to the anonymous > > > module to clear the association cache for the association if the value > > > has changed (calling super in either case). That way you can just do > > > "Sequel::Model.plugin :stale_associations" before defining your models > > > and automatically get the behavior. > > > > Anyone who wants to implement it first is welcome to submit a patch/ > > > pull request. :) > > > I'm working on it. > > > Any given column can act as a foreign key for more than one association > > though. Given that, do you still think that the logic should be in the > > setter method? It would have to iterate over all associations that use that > > column. I think it might be easier to overload the association accessor. > > Overloading the accessor slows down all accesses, which I assume are > much more frequent than setters. It is true that you have to handle > all associations that depend on the foreign key, but that shouldn't be > too difficult (completely untested code): > > # Class instance variables > @stale_associations = {} > include(@stale_associations_module = Module.new) > # for each many_to_one association definition > reflection[:keys].each do |k| > assocs = (@stale_associations[k] ||= []) > assocs << reflection[:name] > @stale_associations_module.class_eval do > unless method_defined?("#{k}=") > define_method("#{k}=") do |v| > super(v) > assocs.each{|a| associations.delete(a)} > end > end > end > end > > Look at the lazy_attributes plugin for a similar idea that uses an > anonymous module.
Just to keep everyone in the loop if you haven't been following on GitHub, jfirebaugh submitted a plugin for this and it was committed a short while ago. Jeremy -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sequel-talk?hl=en.
