For macOS
Postgresql setup
brew install postgresql
brew services start postgresql
Create an app with postgresql
rails new app --database=postgresql
bin/rails db:migrate
rails g controller admin index
Student model represents a single student, so it's singular. The students table holds all of the students, so it's plural.
Create a model
rails g model Student first_name:string last_name:string gender:string
Model validations
https://guides.rubyonrails.org/active_record_validations.html
https://learn.co/lessons/activerecord-lifecycle-reading
List all tables
ActiveRecord::Base.connection.tables
List all columns of a table
ActiveRecord::Base.connection.columns('student').map(&:name)
When creating a new database, the following command tends to be faster
bin/rails db:schema:load
Delete all records in a database table
Post.delete_all
-
File/class-level comments
- Every class definition should have an accompanying comment that describes what it is for and how it should be used.
- A file that contains zero classes or more than one class should have a comment at the top describing its contents.
- All files, including data and config files, should have file-level comments.
-
Function comments
- Every function declaration should have comments immediately preceding it that describe what the function does and how to use it.
- Every function should mention what the inputs and outputs are,
unless it meets all of the following criteria:
- not externally visible
- very short
- obvious
-
Block and inline comments
- Complicated operations get a few lines of comments before the operations commence. Non-obvious ones get comments at the end of the line.
- do not use block comments. They cannot be preceded by whitespace and are not as easy to spot as regular comments.
-
Punctuation, spelling and grammar
- Pay attention to punctuation, spelling, and grammar; it is easier to read well-written comments than badly written ones.
- Comments should be as readable as narrative text, with proper capitalization and punctuation.
- In many cases, complete sentences are more readable than sentence fragments.
- Shorter comments, such as comments at the end of a line of code, can sometimes be less formal, but you should be consistent with your style.
-
TODO comments
- Use TODO comments for code that is temporary, a short-term solution, or good-enough but not perfect.
- TODOs should include the string TODO in all caps, followed by the full name of the person who can best provide context about the problem referenced by the TODO, in parentheses.
- A colon is optional.
- A comment explaining what there is to do is required.
- The main purpose is to have a consistent TODO format that can be searched to find the person who can provide more details upon request.
- A TODO is not a commitment that the person referenced will fix the problem.
# bad
# TODO(TDO): Use proper namespacing for this constant.
# bad
# TODO(orinify122): Use proper namespacing for this constant.
# good
# TODO(Orin): Use proper namespacing for this constant.
- Commented-out code
- Never leave commented-out code in the codebase.