-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better workaround for performance around unknown attributes? #202
Comments
Hi! I guess the problem is not in the method missing itself, but at fact that Ruby has to go through all fields (as you said, there are many of them) and do some work to persist them. There's no "static" way to do this (because attributes are unknown :) ). First thing you can do is to turn it on globally Also, I think we could try to patch the |
Hi, Thanks for the quick reply! I believe the workaround I proposed is more maintainable then using .slice(...) because it doesn't force me to duplicate the list of expected attributes (it can stay in the "models") and it also works for nested attributes without extra effort. Doing Best case scenario would be an extra configuration such as Maybe this would be possible without patching ActiveRecord? When I pass a StoreModel object to the update method, some method must be called so ActiveRecord can convert it into an SQL value? |
To clarify about performance, the difference is very huge. Goes from 70 seconds to less than a second. The payload isn't that huge, it's not like I am trying to persist a 1MB payload, but there are a lot of nested stuffs with unknown attributes |
Got it, makes a lot of sense 🙂 I guess we could make that shorter with minor patch to AR def "#{attribute_name}="(value)
from_value(value).as_json
end Also, it might make sense to allow configuring What do you think? Do you want to try to build it? 🙂 |
I have done some more digging into this and I noticed Let's say I have the following Model
Database organization = { id: 123, name: "Org 1", email: "email@example.com" } Then we go ahead and make a request { id: 1, organization_attributes: { id: 123, email: "new_email@example.com"} } I would be expecting to have my email updated and have the Also, re-assigning those parameters in Overwritting the setter like so def organization=(value)
if self.organization.present? && value.is_a?(Hash)
self.organization.assign_attributes(value)
else
super
end
end will take cake for the issue but will bring back the performance issue. Do you have other idea? |
I might be wrong, but there was a quite common trick in forms when you just always resend all attributes — in that case you don't care about empty values, because old values will always be sent. That's prone to races of course, but works. |
Hi,
Let's say I have an ActiveRecord model
MyModel
with the followingDoing the following
Will be super slow if my payload contains a lot of unknown keys.
Doing the following would fix the performance issue:
Analyzing using
RubyProf
gave me thisIs there a better way?
The text was updated successfully, but these errors were encountered: