Today I Learned

2 posts by radekjędryszczak

Skipp model instance callbacks in Sequel

In ActiveRecord there are several ways to update a record. Each technique / method is described in the documentation, so you are aware of which methods will trigger the callbacks and which ones will skip them. In Sequel it’s not that obvious. And the update, update_all and save - callbacks are triggered in all cases.

It turns out that you need to drop down on a dataset level. Either by:

HuddleScore.where(id: 14900).update(updated_at: DateTime.parse("04/11/2019 14:53"))

or in the following manner:

model_instance.this.update(updated_at: DateTime.parse("04/11/2019 14:53"))

** operator as a way to merge hashes && symbolize_keys

array.group_by(&:something).map do |scheduled_at, form_submissions|
  { scheduled_at: scheduled_at, **form_submissions.first.payload.symbolize_keys }
end

What’s happening here?

The double splat operator acts as a handy way to merge two hashes. But since the payload contains a json with keys being strings, it will throw an exception of TypeError: hash key "name" is not a Symbol

This is because of the Ruby’s implementation internal detail: The keyword arguments, which the double-splat operator is supposed to capture, are expressed in Ruby as Symbols. Hence, the symbolize_keys call.