I've been learning rails for a month now and with all the tutorials that I've gone through, many times they will create more than one controllers and models. I know what controllers and models do, but I still cannot wrap my head around when to create new ones when developing a web app.
2 thoughts on “When should I create more controllers/models?”
I hear ya! I’m just learning too, but I’m thinking after they get so long, large, and hard to follow, go ahead and start a new one.
Data models can be hard and you shouldn’t feel bad about not feeling comfortable along the way. It’s easy to migrate from one data model to the other in Rails so don’t be afraid to expirement.
A guideline: If you want to store metadata about an attribute, it’s time to take that attribute out of the current model and make a new associated model with the metadata as attributes. For example, you have a Car model with number_of_owners:integer attribute. If you want to know a lot of metadata (attributes) about the owners themselves, for example storing the age and gender of each owner, you can migrate towards Car has_many :owners, with an Owner model that has attributes gender:string and age:integer. You can now write a method in your Car model to replace the number_of_owners attribute like this:
def number_of_owners; self.owners.count; end