ruby on rails - How to skip before_update if before_create was called -
i'm trying keep user model "in sync" third party.
so in user.rb:
before_create { thirdparty.create!(user: self) } before_update { thirdparty.update!(user: self) }
the problem arises when i:
user.create!...
as both callbacks invoked (before_create
before_update
).
this how got around behaviour now:
before_create { @before_create_called = true thirdparty.create!(user: self) } before_update { return unless @before_create_called thirdparty.update!(user: self) }
but i'm not confident/comfortable solution. there better way approach this?
edit
i'm sorry, of course mistake, @max williams wanted bottom of this, curious:
i had a:
after_create { a.new() }
which somewhere in bowels did this:
user.toggle(:active)
now this:
user.update_attributes(active: true).
and didn't realize toggle
skips validations , callbacks
since want keep third party synced on each save, want change action based on whether record being created or updated try:
before_save self.new_record? ? thirdparty.create!(user: self) : thirdparty.update!(user: self) end
Comments
Post a Comment