Olympics Warm up
4 min readOct 31, 2020
1. what are AR associations?
In Rails, an association is a connection between two Active Record models. Associations make common operations simpler and easier in your code.class Movie < ApplicationRecord
has_many :actors
endhas_many is a typical AR association. The common associations are has_one, has_many, belongs_to, has_one_through, has_many_through...
2. Understanding associations in a SQL perspective
what does the association do in a SQL level?class User < ApplicationRecord
has_many :tokens
endsay if a User has_many tokes, tokens table will have a column to store user_id, and if we want to find out which are the tokens owned by a user whose id is "123", we can write a query like this:SELECT * FROM tokens WHERE user_id = 123
3. Avoid N+1, and choose wisely between includes and join
includes and joins are used in almost same scenario. Includes uses eager loading whereas joins uses lazy loading..joins will just joins the tables and brings selected fields in return. if you call associations on joins query result, it will fire database queries again(WHICH IS REALLY BAD).:includes will eager load the included associations and add them in memory. :includes loads all the included tables attributes. If you call associations on include query result, it will not fire any queriesblogs = Blog.allauthors = []
blogs.each do |blog|
authors << blog.author.name
endThis is a N+1 query. Revising this with Blog.all.includes(:author) is better than using Blog.all.joins(:author).joins uses lazy loading, and every time when blog call blog.author, it will fire up a new query.
4. Common HTTP status codes
5. Design pattern : MVC
Model
Model is where the application’s data objects are stored. The model doesn’t know anything about views and controllers. When a model changes, typically it will notify its observers that a change has occurred.View
View is what's presented to the users and how users interact with the app.Controller
The controller is the decision maker and the glue between the model and view. The controller updates the view when the model changes. It also adds event listeners to the view and updates the model when the user manipulates the view.
6. Where does the params come from?
params values come from 3 places1. Wildcard => cats/:cat_id2. request body => {cat_id: 12}3. query_string => cats?cat_id=1those all can be accessed by the params[:cat_id]
7. Life cycle of a controller’s instance variable
The controller lives for only one request response cycle.
8. Choose the right method and path
get '/posts' give you all the posts
patch '/posts/:id' change a certain post
post '/posts' create a post in the db
...
9. Metaprogramming
class Dog
def self.eat_food(food)
define_method(food) {puts "eat #{food}"
end
eat_food(:apple)
eat_food(:egg)
eat_food(:orange)
endthe eat_food methods will be executed immediately after ruby load the Dog class, which means all the dog instances will have methods named apple, egg, and orange.
10. CSS
.class_name {
display: flex;
flex-direction: row/column;
justify-content: center/space-around/space-between;
align-items: center;
padding: 100px;
margin: 100px
}1. justify-content: Alignment & Spacing along primary axis center: Align children horizontally centered space-between: Distribute children horizontally evenly across entire width space-around: Distribute children horizontally evenly across entire width (but with space on the edges2. align-items: Vertical: Alignment only along secondary axis (Y-axis) center: Align children vertically centered3. descendent selector: all of the child li of ul will be selected.ul li {
declarations...
}
11. What is ERB and how is it used?
ERBs are just templates consist of HTML, but they are augmented with Ruby code. ERB templates are pretty simple:<% ruby_code_here %> executes Ruby code that does not return anything. For example, conditions, loops or blocks.<%= %> is used when you want to embed the return value into the template. i.e. Something that will actually show up in the HTML.
12. dependent destroy
class User < ApplicationRecord
has_many :posts, dependent: :destroy
end
class Post < ApplicationRecord
belongs_to :user
endSuppose an example where a user has many posts. A user's posts should be destroyed if the user is destroyed; else the posts are said to bewidowed. To do this, we pass the :dependent option to has_many:.Using dependent: :destroy or dependent: :nullify should help clean up child records so that records don't become widowed.