Some Thoughts on CRUD
I sent a helpful PDF on Rails support for CRUD and REST to a buddy of mine. He asked for some clarifications. Here's what I sent to him.
Try to think in nouns, and not verbs. Nouns are your Model classes, things which you can have instances of (User, BankAccount, Book, Login). That last example, Login, is where (hopefully) the light bulb goes off. Consider if you could only Create, Read, Update, or Delete *things* (nouns). How would you do a login?
One way, the old way, is to add a login method on your user controller. Think about what you're saying. You want to call the method (or *verb*) "login" on User. Already you can see we've left the CRUD methods, and a warning bell should go off.
If a user authenticated via User.login, what artifacts are generated? Are you simply returning a boolean? If so, how do you track logins? How do you ask "When was the last time I logged in?"
To solve the problem, you noun-ify the concept of login. Create a Login model, and make a relationship between User and Login.
User has_many :logins
This way, you can Create an instance of Login. No more special verb for login, just Create one. This way, you're tracking all logins as *things* (nouns) and you can do CRUD on them all you want.
The moral of the story is: Think in *things*, not actions. Nouns instead of verbs. Use relationships like `has_many`, `has_many :through`, and `belongs_to` to connect the dots. Then, you are free to have very simple Controllers which are only concerned with CRUD methods.
Once you get these core concepts down (which are just standard Web app best practices), you can investigate Rails' REST support. It's a bunch of conventions to help build CRUD apps.
Try to think in nouns, and not verbs. Nouns are your Model classes, things which you can have instances of (User, BankAccount, Book, Login). That last example, Login, is where (hopefully) the light bulb goes off. Consider if you could only Create, Read, Update, or Delete *things* (nouns). How would you do a login?
One way, the old way, is to add a login method on your user controller. Think about what you're saying. You want to call the method (or *verb*) "login" on User. Already you can see we've left the CRUD methods, and a warning bell should go off.
If a user authenticated via User.login, what artifacts are generated? Are you simply returning a boolean? If so, how do you track logins? How do you ask "When was the last time I logged in?"
To solve the problem, you noun-ify the concept of login. Create a Login model, and make a relationship between User and Login.
User has_many :logins
This way, you can Create an instance of Login. No more special verb for login, just Create one. This way, you're tracking all logins as *things* (nouns) and you can do CRUD on them all you want.
The moral of the story is: Think in *things*, not actions. Nouns instead of verbs. Use relationships like `has_many`, `has_many :through`, and `belongs_to` to connect the dots. Then, you are free to have very simple Controllers which are only concerned with CRUD methods.
Once you get these core concepts down (which are just standard Web app best practices), you can investigate Rails' REST support. It's a bunch of conventions to help build CRUD apps.