🔧 AI Nachrichten OpenAI Targets Work of Wall Street Junior Bankers(10.09.2026 um 21:02 Uhr)
🔧 AI Nachrichten Altman Considers Slowing Down AI Development(11.09.2026 um 20:00 Uhr)
⚠️ Malware / Trojaner / VirenJSCeal Malware Can Bypass Google Authentication Using Stolen Session Cookies(07.09.2026 um 09:53 Uhr)
⚠️ Malware / Trojaner / VirenBengalSEO Poisons Bing Search Results to Deliver MayaBot and Tech Support Scams(08.09.2026 um 10:43 Uhr)
🕵️ SicherheitslückenN-able N-central Pre-Auth RCE Flaw Exploited in the Wild(09.09.2026 um 06:27 Uhr)
🔧 AI Nachrichten OpenAI Targets Work of Wall Street Junior Bankers(10.09.2026 um 21:02 Uhr)
🔧 AI Nachrichten Altman Considers Slowing Down AI Development(11.09.2026 um 20:00 Uhr)
⚠️ Malware / Trojaner / VirenJSCeal Malware Can Bypass Google Authentication Using Stolen Session Cookies(07.09.2026 um 09:53 Uhr)
⚠️ Malware / Trojaner / VirenBengalSEO Poisons Bing Search Results to Deliver MayaBot and Tech Support Scams(08.09.2026 um 10:43 Uhr)
🕵️ SicherheitslückenN-able N-central Pre-Auth RCE Flaw Exploited in the Wild(09.09.2026 um 06:27 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 2 Min Lesezeit
0

Avoid constantize in Rails

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

What do you do when you need to instanciate a class in Rails, but its name is dynamic?



Let's say we want to serialize an object for use in an API payload and we have to handle different versions.



We could have a MySerializer::V1 and MySerializer::V2 classes.



And to instanciate the correct one, we could do something like this:




CODE
serializer_class = "MySerializer::V#{version}".constantize
serializer = serializer_class.new(object)






It works, but we should really avoid it.






It could be unsafe



If the string that is constantized ends up containing user-controlled data, we could be in trouble.



Let's say you are allowing the user to create different record types based on a type attribute.




CODE
params[:record_type].constantize.create!(...)






Your front-end could only send types like Article or Category, but a malicious user could send something like AdminUser and create themself an admin account.



This is highly unlikely, but it's still a risk.






It has bad developer experience




  • It's hard to read and hard to follow

  • You can't use go to definition, forcing you to open the file manually

  • When searching for a class name, you won't find the lines with constantize, giving you a false sense of security






How to do instead ?



You could have a whitelist of classes you allow to be instanciated, but this doesn't solve all problems.



What I like to do is use a hash to map some value with the class you want to call, for instance :




CODE
SERIALIZER_CLASSES = {
"v1" => Myserializer::V1,
"v2" => Myserializer::V2
}

def serializer_for(version)
SERIALIZER_CLASSES[version] || raise "Unknown serializer for version #{version}"
end

serializer_for("v2").new(...)






I like it because it's explicit, easy to read, and allows you to use go to definition or find the line when you search for a class name.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
2 Quellen
Microsoft Phone Link Not Showing Messages on Windows 11? Fix It
1 Quelle
How to Enable Windows 11 Screen Savers
1 Quelle
Post-DEF CON phishing campaign delivered AMOS and NetSupport malware
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Avoid constantize in Rails

Thematisch verwandte Begriffe: Avoid, constantize, Rails · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...