What I've learned Rails in Week 1

Before participating in Tealeaf academy, I practice Rails with Michael's "Ruby on Rails Tutorial". Although the blog project can be completed, but I can't through enough understand a lot of details. You know 'rails generate' does contain a lot of magic which often make me puzzled.

The tealeaf's course is very different. I can progressive learning each knowledge point, be able to understand the meaning of each line of code. Let me talk about the points which I learned these days.

The two most important points:

  1. with the migrations to manipulate the database

  2. use routes.rb to manage routing

1.1 Migrations

Each migration file can be used to modify the database.

with db:migrate, Rails will be adjusted according to changes in migration database files. We can get a new version of the database.

Migrations file are named with timestamp, with db:migrate, Rails record version info to db/development.sqlite3 -- shcema_migrations table.

Use the timestamp as database version name, we can avoids team's guys database version number conflicts.

schema.rb file will record the latest database information(including version information), similar in concept to the snapshot. we can use db:rollback command to roll the database to previous version. You can also see the changes in db/development.sqlite3 -- shcema_migrations table or schema.rb file.

If we don't know how to name the migration file name, we can user the tableize method.

2.0.0p247 :009 > "User".tableize 
=> "users” 

1.2 Assignments

Although you create database with the migrations, but if the associations is not declared inside model, Rails does not generate the corresponding method. There is no way by calling the method to operation the database.

2.0.0p247 :002 > user = User.first 
User Load (0.1ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1 

=> #<User id: 2, username: "Jenny", created_at: "2013-12-01 11:26:16", updated_at: "2013-12-01 11:26:16"> 

2.0.0p247 :003 > user.posts NoMethodError: undefined method `posts' for #<User:0x007f81a215b7f8> 

Add hasmany, belongsto .etc to model, the associations been build, than we can operation the database.

Sometimes, the default method name can not be a good expression related entities. We can modify the name of the method like this.

belongs_to :creator, foreign_key: 'user_id', class_name: ‘User' 

Rails offers two different ways to declare a many-to-many relationship between models. hasmany :through and hasandbelongsto_many. Many times we may want to set some things in join table, so chose hmt we can have better adaptability.

2.1 Resources / routes

The Rails router recognizes URLs and dispatches them to a controller's action.

We write code in routes.rb like this:

get '/posts', to: 'posts#index' 

get '/posts/:id', to: 'posts#show' 

... 

When Rails application receives an incoming request for:

GET /posts 

it asks the router to match it to post controller index action.

we can use rake routes to show witch routes in out application.

rake routes

If we don't want to create so many routes manually, we use the resource declares index, show, new, edit, create, update and destroy actions in a single line of code.

resources :posts 

The resources will generate all routes, So use it without restriction is very dangerous. We can use only or except to constraints it.

resources :posts, except: :destroy 

Others

pry
  1. add pry to Gemfiile and bundle install it

    gem 'pry', :group => :development 
    
  2. Insert 'binding.pry' in where you want to debug. when Rails server run to the 'binding.pry' code, it will open a pry terminal, we can debug with the terminal.

mass assignment

We can use mass assignment to set multiple attributes at once.

post = Post.new(title: "Last Post", description: "This is Last", user_id: 1) 

# In ruby if the last variable is a hash, we don't need the curly braces. 

Thanks to Chris, Kevin, TAs and my little friends. Learning together with yours, I get a lot of fun. Thank you.

Neil Wang

产品经理。喜欢摄影,滑板,电影。微信公众号:NeilOnly