Rails3入门锦集(5) 第二个模型: 一对多关系
入门锦集1-8都来自官方的翻译:http://edgeguides.rubyonrails.org/getting_started.html
?
?
1. 添加第二个模型
在前面的教程中,我们已经学会的使用脚手架快速搭建一个简单的应用,毕竟脚手架不能做任何事情,现在我们需要在应用中添加第二个模型了。
?
模型在rails中使用单数形式,而其相关的数据库将使用复数名称。
?
那么对于一个博客来说,评论总是少不了的,我们现在就要创建一个 Comment 模型。对于大多数的 rails 程序员来说,一般都是通过rails的生成器来生成模型,在这里我们也一样:
?
$ rails g model comment commenter:string body:text post:references
?
该命令将创建4个文件:
?
class Comment < ActiveRecord::Base belongs_to :postend?初看之下,你可能会觉得它与post模型很相识,不同之处在于它多了一行 belongs_to 声明,而这将会建立它与post模型之间的关联, 那么在后面的教程中我们将详细学习。
class CreateComments < ActiveRecord::Migration def self.up create_table :comments do |t| t.string :commenter t.text :body t.references :post t.timestamps end end def self.down drop_table :comments endend?与之前的rails版本不同,这里多了一个 t.references 定义。t.references :post 将建立一个posts表的外键 post_id, 值得注意的是这里的 :post 指的模型而不是表,这点一定要搞清楚。现在运行迁移操作把:
class Post < ActiveRecord::Base validates :name, :presence => true validates :title, :presence => true, :length => { :minimum => 5 } has_many :commentsend?现在双方都已经建立起一系列的关系,打个比方,假如你有一个实例变量 @post 包含一条 post,那么你可以使用 @post.comments 来检索所有该 post 中的 comment。resources :posts do resources :commentsend?这将使 comments 资源作为 posts 资源的嵌套资源。而这也反映出 post 与 comment 之间的关系。
<p name="code">class CommentsController < ApplicationController def create @post = Post.find(params[:post_id]) @comment = @post.comments.create(params[:comment]) redirect_to post_path(@post) endend?你可能会发现这里要比之前的posts控制器中稍微复杂一些,这就是你之前设定嵌套的片效应。每一个评论的请求需要追踪该评论所附加的Post,所以find首先会找出该Post对象。
<p class="notice"><%= notice %></p> <p> <b>Name:</b> <%= @post.name %></p> <p> <b>Title:</b> <%= @post.title %></p> <p> <b>Content:</b> <%= @post.content %></p> <h2>Comments</h2><% @post.comments.each do |comment| %> <p> <b>Commenter:</b> <%= comment.commenter %> </p> <p> <b>Comment:</b> <%= comment.body %> </p><% end %> <h2>Add a comment:</h2><%= form_for([@post, @post.comments.build]) do |f| %> <div class="field"> <%= f.label :commenter %><br /> <%= f.text_field :commenter %> </div> <div class="field"> <%= f.label :body %><br /> <%= f.text_area :body %> </div> <div class="actions"> <%= f.submit %> </div><% end %> <br /> <%= link_to 'Edit Post', edit_post_path(@post) %> |<%= link_to 'Back to Posts', posts_path %> |?现在你可以在你的博客中添加post和评论了,并且把它们显示在正确的地方。