I have a model House that has many boolean attributes, like has_fireplace, has_basement, has_garage, and so on. House has around 30 such boolean attributes. What is the best way to structure this model for efficient database storage and search?
I would like to eventually search for all Houses that have a fireplace and a garage, for example.
The naive way, I suppose, would be to simply add 30 boolean attributes in the model that each corresponds to a column in the database, but I’m curious if there’s a Rails best practice I’m unaware of.
Your ‘naive’ assumption is correct – the most efficient way from a query speed and productivity perspective is to add a column for each flag.
You could get fancy as some others have described, but unless you’re solving some very specific performance problems, it’s not worth the effort. You’d end with a system that’s harder to maintain, less flexible and that takes longer to develop.