Lundie wrote:
Hi Everyone,
company has_many phone_numbers
company has_many contacts
company has_one status
.... and so on
I need to be able to have the plugin version each associated model
automatically.
Does anyone know of a solution for this?
I haven't heard of anything like that.
But I can't imagine you can adapt ActsAsVersioned to your needs,
because it works with a different table, so associations are going to
break.
Perhaps a more bespoke solution;
add_column :companies, :versioned_at, :datetime, :default => nil
add_column :companies, :version_of, :integer, :default => nil
Now when we "version" it, we go.
class Company
has_many :versions, :class_name => "Company", :foreign_key =>
"version_id", :order => "companies.versioned_at DESC"
belongs_to :version_of, :class_name => "Company"
def version!
transaction do
versioned = Company.new(self.attributes.merge(:versioned_at =>
Time.now, :version_of => self.id)
# go through each association and clone the attribs, pointing at
versioned
self.phone_numbers.each do |phone_number|
versioned.phone_numbers.build(phone_number.attributes)
end
...
self.save!
end
end
end
something like that maybe?
(totally untested code)
Matthew Rudy Jacobs
http://funchforlunch.blogspot.com