1. now, we will add User model to our app, so that we can register, sign in, etc.
rails g model User name:string email:string
note:
when generate controller, we use plural format: rails g controller Users new
but when gen model, we use singular: rails g model User name:string email:string
2. one of the result of this generate, is that it will create a new file call migration.
a migration is a way to alter database by incremental way.
the file name of the migration is pre-fixed by a timestamp, indicating when the migration is created.
3. after done the create of user model, let's go to look at the code in the migration:
a. def sef.up, means this is a class method.
b. create_table is a helper method.
c. this helper method accept a block, passing a param of the table object.
d. t.string :name, create a column
d. t.timestamps, create two time columns, created_at and updated_at
note, the model in singular, but the table name is always plural, since there are many users.
4. sqlite is using a simple file to store database, not like other database.
the data file is just
db/development.sqlite3
db/test.sqlite3
5. if you did migrate, then you regret, then you can use the self.down method inside migration:
rake db:rollback
this will call the self.down method, and drop the table.
the rollback and self.down method will be often used in this senario:
you forget to add a column, but you don't want to take the trouble of making another migration!!!
then rollback, and then add the column to the migration file, then migrate again!!
So easy!! amazing
But sometimes, you still have to add column to existing table, we will talk of it later.
6. if you want to browse the SQLite database easily, you can use SQLite database browser program.
it can easily download from internet.
7. there is a gem called "annotate", this gem will add a command:
annotate --position before
this command will add comments to model files, these comments list all column inside this model, for example:
# == Schema Information # # Table name: users # # id :integer not null, primary key # name :string(255) # email :string(255) # created_at :datetime # updated_at :datetime class User < ActiveRecord::Base end
8. next, we will tell rails which attributs are accessible by outside users:
attr_accessible :name, :email
note, the use of attr_accessible, will also add a great advantage that it can prevent from mass assignment vulnerability. this is a very common hold in web app.
发表评论
-
12.3.3 scaling issue of the status feed
2011-10-30 17:54 824the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8581. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 9131. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8791.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7551. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 9001. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 7031. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 11071. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 745in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 774in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 665again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 7091. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 737whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 7501. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 727There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 658start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 645a session is a semi-permanent c ... -
what test framework should you use?
2011-10-11 16:56 0for integration test, i have no ... -
what test framework should you use?
2011-10-11 16:56 0<p>for integration test, ... -
8.4 rspec integration tests
2011-10-11 16:53 721in integration test, you can te ...
相关推荐
2. cross-database platform: only need to give a definition accoding to the familiar database platform,BDB can Automatically intall and Arbitrarily move the database in these database platforms. ...
2. cross-database platform: only need to give a definition accoding to the familiar database platform,BDB can Automatically intall and Arbitrarily move the database in these database platforms. ...
快速的将3d软件建立的地形物件转换成Unity的专用地形,强大的转换功能将...• Manually adding models or automatic detection of all models in the layer; • No need to manually add the components of physics.
America’s wireless industry is ready to invest $275 billion to deploy next-generation 5G networks — creating 3 million new jobs and adding $500 billion to our economy, according to Accenture.
Module 8: Introduction to Data Analysis [removed]DAX)This module describes how to use DAX to create measures and calculated columns in a tabular data model.Lessons DAX Fundamentals Using DAX to ...
With this book, a stellar author team covers the new controls in the AJAX toolbox, the back button history, and script combining, and they also examine the new capabilities of WCF including changes to ...
Building a Spatial Database in PostgreSQL: David Blasby Refractions Research PostGIS aims to be an “OpenGIS Simple Features for SQL” compliant spatial database; Adding spatial extensions to ...
This book will explore several modules that will allow you to do everything from adding calendars and events to integrating content from Flickr and YouTube. Each chapter adds some features to your ...
标题“Taking the pain out of adding a horizontal scrollbar to a listbox”指向的就是这样一个问题:如何优雅地为列表框添加水平滚动条,以提升用户体验。这里我们将详细探讨这个主题,并提供一些实践技巧。 ...
Searchdialogs arose from an idea by Eric ten Westenend to allow adding search capabilities to a database application in an easy way. Not reprogramming a search functionality over and over again, not ...
The examples illustrate how to carry out some of the most common database operations, including creating a database and table, loading data in a CSV input file into a database table, updat‐ ing ...
cognos_pp_infrastructure_adding_and_removing_ui_sections_in_cognos_connection.pdf,可以定义cognos connection 页面,比如新增链接或者隐藏某些链接等
* Add and modify models, views and controllers to perform CRUD operations against the database. * Use client-side and server-side validation. * Secure the controllers, actions and view content with ...