🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
🕵️ SicherheitslückenBurn Out, Or Fade Away(14.09.2026 um 14:25 Uhr)
🪟 Windows TippsAmazon Prime Big Deal Days: October 6 to October 7, 2026(15.09.2026 um 11:36 Uhr)
🪟 Windows TippsSpotify(15.09.2026 um 11:30 Uhr)
🪟 Windows TippsKB5129194 Windows 11 26H1 Out of Band Update - Deskmodder.de(14.09.2026 um 19:25 Uhr)
🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
🕵️ SicherheitslückenBurn Out, Or Fade Away(14.09.2026 um 14:25 Uhr)
🪟 Windows TippsAmazon Prime Big Deal Days: October 6 to October 7, 2026(15.09.2026 um 11:36 Uhr)
🪟 Windows TippsSpotify(15.09.2026 um 11:30 Uhr)
🪟 Windows TippsKB5129194 Windows 11 26H1 Out of Band Update - Deskmodder.de(14.09.2026 um 19:25 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
1 Quelle
The Gemini desktop app is now available for Windows
1 Quelle
Burn Out, Or Fade Away
1 Quelle
Windows 11 KB5129195 is out after Microsoft confirms major issues with the September 2026 update, but it won’t fix AMD GPU errors
Ä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 ...