On Thursday, April 19, 2012 9:43:49 AM UTC-7, Rodrigo Rosenfeld Rosas wrote:
> For example, take a look at this AR model excerpt: > > after_update :remove_options_if_changed_type_was_options > private > def remove_options_if_changed_type_was_options > options.clear if type_changed? && type_was == 'options' > end > > I've even asked for a type_was? method in a Rails pull request at that > time: > > https://github.com/rails/rails/pull/5763 > Sequel currently doesn't provide a similar feature, as it doesn't save previous values of columns. It's probably not difficult to add a plugin that would do so, though. > So, I tried this to get a similar result in Sequel: > > private > def after_update > super > options_dataset.delete if type_was_options? > end > > def data_type_was_options? > changed_columns.include?(:type) && type != 'options' > end > Yes, I know about the warnings in the documentation: > > "This isn't completely accurate, as it could contain columns whose values > have not changed." > "isn't completely accurate" refers to this: m = Model.new(:name=>'a') m.changed_columns # => [] m.name = 'b' m.name = 'a' m.changed_columns # => [:name] I think ActiveModel::Dirty has the same issue, though that could theoretically be fixed as it has access to the initial value. Although not ideal, that is ok. But the issue is that my spec won't pass. > When I try to print "changed_columns" it is empty. Maybe this information > is reset after a save. > changed_columns is cleared during the save. Note that you can access @columns_updated inside the after_update hook to get the data used in the UPDATE statement. However, even if you did that, I'm not sure if your Sequel code would do what you want. Do you want to run options_dataset.delete only if the value of the type column was changed from "options" to something else during the update? > I've read somewhere in this group that such feature wasn't requested > before, so I'm requesting it now :P > > Could I ask you to include an official "dirty" plugin for Sequel::Model? > One that would even work in after-hooks? And that would be completely > accurate whatever it means? > I'm not opposed to adding one. It will probably not be exactly like ActiveModel::Dirty, though. I'll probably just give the ability to get the previous value, so you could do: def after_update super options_dataset.delete if type_was_options? end def type_was_options? initial_value(:type) == 'options' && @columns_updated[:type] != 'options' end Note that if you have such an option, a better way to do what you want is using a database trigger. Jeremy -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/sequel-talk/-/eZ5j0W06tMcJ. 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.
