This is a review on what I have been through in Week 6 of TeaLeaf Academy.
Rails follow Active Record pattern to perform ORM duties. What’s the Active Record pattern?
- classes to tables
- objects to rows of data within that table
- getters/setters(attributes) to columns in that table
We can create, retrieve, update, and delete the object instances by altering the database table.
To create a table in the database, we can use migrations, rails generate migration create_posts
. In the migration file,
1 2 3 4 5 6 7 8 9 10 11 |
|
After we run rake db:migrate
in the command line, it will create six columns in the database,
- primary key: id
- url
- title
- description
- created_at
- updated_at
Then in the app/models folder, we will create the corresponding model,
1 2 |
|
The file name will be in singular form, post.rb
.
Tips on table name: use the tableize method on the class name. For example, 'Post'.tableize
=> “posts”.
If we have a Comment model and there is a 1:M association between Post and Comment, a foreign_key
column - post_id
need to be added to the comments table and it will point to the primary key column in the posts table. Then in the Post and Comment model file, we will do the following changes.
1 2 3 4 5 6 7 |
|
If there is a Category model and it has a M:M association with Post, a joint model and table will be needed to build up the association. The model file name will be post_category.rb
and class name will be PostCategory
. To get the table name, 'PostCategory'.tableize
and the output is ‘post_categories’. There will be two foreign key columns in the table, post_id
and category_id
. In the model file, we will set up the has_many :through
.
1 2 3 4 5 6 7 8 9 10 |
|
To change the default association name,
1 2 3 |
|
This will make post.creator
work.
Keyword resources
can be used to generate routes.
resources :posts
will make the following routes available,
1 2 3 4 5 6 7 8 9 |
|
To create nested routes like /posts/:id/comments/:id
, we can use nested resources,
1 2 3 |
|
except
and only
keywords can be used to add constrains on the routes.