RUBY ON RAILSTM 3 TUTORIAL笔记 2

chapter6 Modeling and Viewing Users part 1

1.p195The default library for interacting with the database is called Active Record. The name comes from the “active record pattern”, identified and named in Patterns of Enterprise Application Architecture by Martin Fowler.
2.p197Note that, in contrast to the plural(复数的) convention for controller names, model names are singular(单数的): a Users controller, but a User model.
3.p197Migrations(迁移) provide a way to alter the structure of the database incrementally(递增地), so that our data model can adapt to changing requirements.
4.p198Let’s focus on the self.up method, which uses a Rails method called create_table to create a table in the database for storing users. (The use of self in self.up identifies
it as a class method. )
5.p198The final line in the block, t.timestamps, is a special command that creates two magic columns called created_at and updated_at, which are timestamps that automatically record when a given user is created and updated.

6.p199You’ve probably inferred that running db:migrate executes the self.up command in the migration file. What, then, of self.down? As you might guess, down migrates down, reversing the effects of migrating up. In our case, this means dropping the users table from the database.
7.p203When working at the console, it’s useful to keep an eye on the development log, which records the actual low-level SQL statements being issued by Active Record, as shown in Figure 6.4. The way to get this output at a Unix command line is to tail the log:

$ tail -f log/development.log

8.p205If you’ve been tailing the development log, you may have noticed that no new lines have shown up yet. This is because calling User.new doesn’t touch the database; it simply creates a new Ruby object in memory.
9.p205关于date
In case you’re curious about "2010-01-05 00:57:46", I’m not writing this after midnight; the timestamps are recorded in Coordinated Universal Time (UTC), which for most practical purposes is the same as Greenwich Mean Time. From the NIST Time and Frequency FAQ: Q: Why is UTC used as the acronym for Coordinated Universal Time instead of CUT? A: In 1970 the Coordinated Universal Time system was devised by an international advisory group of technical experts within the International Telecommunication Union (ITU). The ITU felt it was best to designate a single abbreviation for use in all languages in order to minimize confusion. Since unanimous agreement could not be achieved on using either the English word order, CUT, or the French word order, TUC, the acronym UTC was chosen as a compromise.
10.p209It’s worth noting that, once you have defined some attributes as accessible using attr_accessible (Section 6.1.2), only those attributes can be modified using
update_attributes. If you ever find that your models mysteriously start refusing to update certain columns, check to make sure that those columns are included in the call
to attr_accessible.
11.p211As discussed briefly in Section 2.3.2, the use of validates is characteristic of Rails 3. (In Rails 2.3, we would write validates_presence_of :name instead.)
12.p213The next line is a before(:each) block; this was covered briefly in an exercise (Listing 3.33), and all it does is run the code inside the block before each example—in this case setting the @attr instance variable to an initialization hash.
13.????p214Listing 6.9 shows an example of a pending spec using an explicit call to the pending method; in this case, since we have included only the it part of the example,

it "should require a name"

RSpec infers(推理,推断) the existence of a pending spec.
pending spec???
14.****p214Pending specs are useful as placeholders for tests we know we need to write at some point but don’t want to deal with right now.
15.???p215no_name_user.should_not be_valid
16.*****p225Luckily, the solution is straightforward to implement; we just need to enforce uniqueness at the database level as well.Our method is to create a database index on the email column, and then require that the index be unique.
17.p229As you can see, Rails provides a Rails object with an env attribute and associated environment boolean methods. In particular, Rails.env.development? is true only
in a development environment
18.p233The REST design style emphasizes representing data as resources that can be created, shown, updated, or destroyed—four actions corresponding to the four fundamental operations POST, GET, PUT, and DELETE defined by the HTTP standard (Box 3.1).
19.****p233We can get the REST-style Users URL to work by adding users as a resource to config/routes.rb
20.*****p236Using params[:id] to pull out the id from the URL is a common Rails idiom. In particular, we used the code:

User.find(params[:id])

 

chapter7 Modeling and Viewing Users part 2

1.













 

 

 

 

 

 




 

posted on 2012-06-03 16:34  jiyiyouxin  阅读(113)  评论(0)    收藏  举报