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

rails4 6.2 Associating Models

阅读更多

Active Record关联性让你能简单地声明两个模型间的关系。这个例子就是关于 评论 和 文章 这两个模型间的关系, 你可以通过下面的说明来了解两者之间关系:

  • 每一个评论都属于一篇文章的。
  • 每一篇文章可以拥有许多个评论的。

事实上,这已经非常接近其语法结构了,Rails通常就是这样来声明其关联性的。你已经在 评论 的模型中看到过这一行代码了 app/models/comment.rb,使得每一个评论都属于一篇文章:

classComment < ActiveRecord::Base

  belongs_to :article

end

你将需要去编辑这个文件 app/models/article.rb,增加关联性的另一边:

classArticle < ActiveRecord::Base

  has_many :comments

  validates :title, presence: true,

                    length: { minimum: 5}

end

这两个声明能够产生一系列的自动化行为。比如,如果你拥有一个实例变量@article包含了一篇文章,你可以检索到所有属于这篇文章的评论,类似于一个数组 @article.comments.

p.s. 想要了解更过的Active Record关联性,可以参考 Active Record Associations

 

original text: http://guides.rubyonrails.org/getting_started.html#associating-models

 

— end

0
0
分享到:
评论

相关推荐

    Rails 4 in Action(Manning,2015)

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

    Agile Web Development with Rails 4

    Ruby on Rails helps you produce high-quality, beautiful-looking web applications quickly. You concentrate on creating the application, and Rails takes care of the details., Tens of thousands of ...

    Rails 4 Test Prescriptions.pdf

    Rails 4 Test Prescriptions.pdf

    rails-translate-models

    gem 'rails-translate-models' 设置 提供的所有示例都将基于标题和正文作为翻译属性的文章模型。 移民 对于每个模型,您必须为翻译创建一个表,您可以在模型迁移时进行,例​​如: class CreateArticles &lt; ...

    Beginning Rails 4

    Beginning Rails 4

    Rails 4 Test Prescriptions

    Rails test described by RSpec

    Rails 4 in Action, Second Edition.pdf

    Rails 4 in Action, Second Edition.pdf

    rails-activerecord-models-and-rails-readme-v-000

    Active Record是Rails用来管理应用程序模型方面的内置ORM。 什么是ORM? ORM是一个对象关系映射系统,从本质上讲,这是一个模块,它使您的应用程序能够以方法驱动的结构来管理数据。 这意味着您可以利用方法而不是...

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

    [Pragmatic Bookshelf] Crafting Rails 4 Applications Expert Practices for Everyday Rails Development (E-Book) ☆ 图书概要:☆ Get ready to see Rails as you've never seen it before. Learn how to ...

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

    Crafting Rails 4 Applications.pdf

    一本Rails 4开发进阶教程,适合有一定开发经验的Ruby on Rails开发人员阅读

    Rails 4 Test Prescriptions(Pragmatic,2014)

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

    The Rails 4 Way

    学习ruby!

    Ruby on Rails4 api.chm

    Ruby on Rails4 api.chm ruby on rails 4 最新api chm版本,方便大家阅览!

    todo-rails4-angularjs, Rails 4和AngularJS集成的展示.zip

    todo-rails4-angularjs, Rails 4和AngularJS集成的展示 Rails 4和AngularJS集成的展示这是一个待办事项列表管理应用程序,用 Rails 4和AngularJS编写。 任务支持CRUD操作以及drag&拖放排序和通过弹出式日历设置的...

    The Rails 4 Way 英文版.pdf

    Ruby on Rails框架是Ruby的一个杀手级应用框架,可以敏捷开发Web应用,是目前为止开发效率最高的Web开发技术

    Crafting Rails 4 Applications

    Crafting Rails 4 Applications

    THE RAILS 4 WAY, 3RD EDITION

    Book Details Publisher: Addison-Wesley Professional By: Obie Fernandez ISBN-10: 0321944275 ISBN-13: 978-0321944276 Year: June 8, 2014 Pages: 912 Language: English File size: 4 MB File format: PDF

Global site tag (gtag.js) - Google Analytics