是时候为应用程序增加第二个模型了。第二个模型将会用来处理关于文章的评论Comment。
6.1 Generating a Model
我们将会看到之前用过的同样的生成器,在创建Article模型的时候。这一次我们将会创建一个Comment模型,来关联到文章的评论。在命令窗口运行这个命令:
$ rails generate model Comment commenter:string body:text article:references |
这个命令将会产生四个文件:
文件 |
目的 |
db/migrate/20140120201010_create_comments.rb |
数据迁移用来在你的数据库中创建关于评论的数据表 (你的文件名将会包括一个不一样的时间戳) |
app/models/comment.rb |
The Comment model |
test/models/comment_test.rb |
Testing harness for the comments model |
test/fixtures/comments.yml |
Sample comments for use in testing |
首先,来看一下 app/models/comment.rb:
classComment < ActiveRecord::Base belongs_to :article end |
这个和之前你看到的Article模型非常相似。不一样的地方是这行 belongs_to :article,这是建立一个与Active Record的关联。你将会在下一部分的内容中学到一些关于关联的知识。
另外对于模型,Rails同样弄了一个数据迁移来创建一个相对应的数据表:
classCreateComments < ActiveRecord::Migration defchange create_table :commentsdo|t| t.string :commenter t.text :body
# this line adds an integer column called `article_id`. t.references :article, index: true
t.timestamps end end end |
这个t.references建立一个外键用来联系这两个模型。对于这个关联的索引同样被创建在这一列上面。继续来运行这个migration命令:
$ rake db:migrate |
Rails是相当的智能,来产生这些数据迁移的命令,还没在现在的数据库上面运行过,因此在这个例子中你将会看到:
== CreateComments: migrating ================================================= -- create_table(:comments) -> 0.0115s == CreateComments: migrated (0.0119s) ======================================== |
原文来源:http://guides.rubyonrails.org/getting_started.html#adding-a-second-model
—end
相关推荐
Rails 4 in Action, Second Edition.pdf
Summary 94 Chapter 6: Creating Navigation Bars 95 Getting started with a navigation bar 95 Navigation bar helper classes 102 Adding a navigation bar to the Rails application 103 Summary 105 Chapter 7:...
Now in version 4, Rails is mature and powerful, and to use it effectively you need more than a few Google searches. You'll find no substitute for the guru's-eye-view of design, testing, deployment, ...
We still start with a step-by-step walkthrough of building a real application, and in-depth chapters look at the built-in Rails features. This edition now gives new Ruby and Rails users more ...
This second edition is updated to cover Angular 2 - a completely reworked front-end framework - and dives into new Postgres 9.5 features such as UPSERT. Also new is Webpack coverage, to develop the ...
唔,1分应该还是有人下的吧,共同学习进步,Ruby on Rails is an open source web framework.... "Rails 4 in Action" is a fully-revised second edition of "Rails 3 in Action." This hands-on, compreh...
Beginning Rails 4
Rails test described by RSpec
Rails 4 Test Prescriptions.pdf
This second edition of the bestselling Crafting Rails Applications has been updated to Rails 4 and discusses new topics such as streaming, mountable engines, and thread safety. ☆ 出版信息:☆ [作者...
Test the component parts of a Rails application, including the back-end model logic and the front-end display logic. With Rails examples, use testing to enable your code to respond better to future ...
学习ruby!
一本Rails 4开发进阶教程,适合有一定开发经验的Ruby on Rails开发人员阅读
Rails 4 Test Prescriptions is a comprehensive guide to how tests can help you design and write better Rails applications. In this completely revised edition, you'll learn why testing works and how to...
rails常用命令,例如新建rails项目,新建controller、model 等等
rails-angular-postgres-and-bootstrap-second-edition 英文原版
Crafting Rails 4 Applications
Ruby on Rails4 api.chm ruby on rails 4 最新api chm版本,方便大家阅览!