The Easiest Way to Peek Inside Any Rails Model
Want to peek inside your model instance? This one method shows everything.
✅ The One-Liner
product.attributes
➡️ Returns a full hash of all model fields and their values:
{
"id"=>1,
"name"=>"Shoes",
"price"=>99.0,
"created_at"=>...,
"updated_at"=>...
}
🔍 Why It’s Useful
- Quickly inspect an object’s data
- Serialize easily to JSON or logs
- Great for debugging and dynamic APIs
⚡ Pro Tip
Exclude sensitive fields like this:
product.attributes.except("created_at", "updated_at")
Or convert to JSON for APIs:
product.attributes.to_json
🧾 TL;DR
Task | Shortcut |
---|---|
See all fields in model | product.attributes |
Exclude some fields | .except("created_at", ...) |
Convert to JSON | .to_json |