`
jiajie0531
  • 浏览: 28822 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

rails4 6 Adding a Second Model

阅读更多

是时候为应用程序增加第二个模型了。第二个模型将会用来处理关于文章的评论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

    ### Rails 4 in Action, 第二版:关键知识点解析 #### 一、Rails 4简介与新特性 **Rails 4 in Action, 第二版** 是一本深入介绍Ruby on Rails框架的专业书籍。该书由Ryan Bigg、Yehuda Katz、Steve Klabnik和...

    The Rails 4 Way

    ### 关于《The Rails 4 Way》的知识点总结 #### 标题:The Rails 4 Way 这本书主要讲述了Ruby on Rails 4版本的核心特性和最佳实践。Ruby on Rails(简称Rails)是一个用Ruby语言编写的开源全栈Web应用框架。本书...

    Rails.Angular.Postgres.and.Bootstrap.2nd.Edition

    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 ...

    Agile Web Development with Rails 4

    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 ...

    Rails 4 in Action

    唔,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...

    Ruby on Rails 4 Tutorial 中文版

    在阅读《Ruby on Rails 4 Tutorial》的过程中,你将深入理解MVC(Model-View-Controller)架构模式,它是Rails的核心设计思想。Model代表数据模型,负责与数据库交互;View是用户界面,展示数据;Controller作为两者...

    Beginning Rails 4

    ### Beginning Rails 4 #### 关于本书 《Beginning Rails 4》是一本旨在为初学者提供全面、系统性介绍 Ruby on Rails 框架的书籍。本书覆盖了从安装环境到构建完整 Web 应用程序的全过程,并深入探讨了 Rails 的...

    Agile Web Development with Rails 4th edition(敏捷Web开发与Rails:程序指南 第四版)

    6. **Rails 4 API Changes**: Rails 4在API上进行了一些改变,如`update_attributes`变为`update`,`destroy`方法现在返回被销毁的对象,这些变化需要开发者了解并适应。 7. **Migrations and Database Schema**: ...

    Rails项目源代码

    Ruby on Rails,通常简称为Rails,是一个基于Ruby编程语言的开源Web应用框架,遵循MVC(Model-View-Controller)架构模式。这个“Rails项目源代码”是一个使用Rails构建的图片分享网站的完整源代码,它揭示了如何...

    Crafting Rails 4 Applications

    ### 关于《Crafting Rails 4 Applications》的关键知识点解析 #### 标题解析:Crafting Rails 4 Applications - **Rails 4**:Rails 4是Ruby on Rails框架的一个版本,该版本在2013年发布。Ruby on Rails(简称...

    Rails 3 in Action

    3. **MVC架构**:Rails采用Model-View-Controller架构,书中详细解释了模型(Model)、视图(View)和控制器(Controller)之间的交互与职责。 4. **路由**:Rails的路由系统允许灵活地定义URL到控制器动作的映射,...

    [Rails] Crafting Rails 4 Applications (英文版)

    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. ☆ 出版信息:☆ [作者...

    rails 5 test prescriptions build a healthy codebase

    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 ...

Global site tag (gtag.js) - Google Analytics