Skip to content

Instantly share code, notes, and snippets.

@eLafo
Last active June 10, 2019 14:53
Show Gist options
  • Save eLafo/106905c0b1e6d72722c401515de45502 to your computer and use it in GitHub Desktop.
Save eLafo/106905c0b1e6d72722c401515de45502 to your computer and use it in GitHub Desktop.
class Foo
# Do some stuff...
# We want to update a field from another model with a value from this model
after_commit :update_bar_field
# Do more stuff...
private
def update_bar_field
# We want to update Bar#bar_field_2 with the value of Foo#foo_field_2
# Bar and foo are related by bar_field_1 and foo_field_1
# (yes, we might use associations instead, but this is an example ;-) )
Bar.where(bar_field_1: foo_field_1).update(bar_field_2: foo_field_2)
end
end
class Foo
# Do some stuff...
# We want to update a field from another model with a value from this model
after_commit :update_bar_field
# Do more stuff...
private
def update_bar_field
# We want to update Bar#bar_field_2 with the value of Foo#foo_field_2
# Bar and foo are related by bar_field_1 and foo_field_1
# (yes, we might use associations instead, but this is an example ;-) )
Bar.by_bar_field_1(foo_field_1).update(bar_field_2: foo_field_2)
end
end
class Foo
include Wisper::Publisher
# Do some stuff...
# We want to update a field from another model with a value from this model
after_commit -> (foo) { foo.broadcast(:foo_saved, foo.foo_field) }
end
class Bar
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment