sorcery icon indicating copy to clipboard operation
sorcery copied to clipboard

User callbacks not working for sourcery

Open bdandy opened this issue 9 years ago • 2 comments

I found an issue that callbacks not working for user when it updates last_activity_at, or whenever you have a call @sourcery_adapter.update_attribute. I even don't understand why that exists, at least for active record. I'll write a fix for this issue.

Why this is important? For example for real-time update via websockets user status.

bdandy avatar Jul 24 '15 00:07 bdandy

Hi @Bogdan-D,

I don't remember specific issue when it was posted, but I think the reason is that the update_attributes method in Rails saves the whole record, not only the attributes you specify. What I mean is that if I have a following User object:

=> #<User:0x007fe51213b970 id: 1, name: "Bob", email: "[email protected]">

and I run following code:

user.name = "Charlie"
user.update_attributes(email: "[email protected]")

the reloaded user object will be following:

=> #<User:0x007fe51213b970 id: 1, name: "Charlie", email: "[email protected]")

which means that both fields were saved to database, while I wanted to save only one.

arnvald avatar Jul 28 '15 02:07 arnvald

Hi @arnvald What about this http://apidock.com/rails/ActiveRecord/Relation/update, it will run callbacks, validations and update only needed attributes without saving model. So resulting code may be

user.name = "Charlie"
User.update(user.id,email: "[email protected]")

Or we could just invoke :save callback manually

bdandy avatar Jul 28 '15 18:07 bdandy