【文件属性】:
文件名称:显示关联Rails休斯敦网站111918
文件大小:59KB
文件格式:ZIP
更新时间:2021-02-26 02:43:14
Ruby
显示关联Rails
目标
学完本课后,您应该能够...
创建has_many和belongs_to关联。
通过控制台和db/seeds.rb构建关联的数据。
使用关联提供的方法查询关联数据。
将关联数据嵌入视图中。
遍历显示单个实例的视图内的关联数据。
博客类别
在本课程中,我们将设置一个博客管理面板,以便可以创建Post对象,将其与Category对象相关联,并按Category列出。
模型
首先,就像上一课一样,我们将建立关联的模型:
# app/models/post.rb
class Post < ActiveRecord :: Base
belongs_to :category
end
# app/models/category.rb
class Category < ActiveRecord :: Base
has_many :posts
end
种子数据
【文件预览】:
displaying-associations-rails-houston-web-111918-master
----.gitignore(642B)
----app()
--------mailers()
--------assets()
--------models()
--------helpers()
--------views()
--------controllers()
----config.ru(153B)
----.rspec(11B)
----LICENSE.md(1KB)
----bin()
--------rails(141B)
--------setup(920B)
--------bundle(129B)
--------spring(507B)
--------update(782B)
--------rake(90B)
----CONTRIBUTING.md(2KB)
----db()
--------migrate()
--------seeds.rb(343B)
--------schema.rb(1KB)
----public()
--------robots.txt(202B)
--------422.html(2KB)
--------favicon.ico(0B)
--------500.html(1KB)
--------404.html(2KB)
----spec()
--------models()
--------views()
--------spec_helper.rb(4KB)
--------rails_helper.rb(3KB)
--------controllers()
----README.md(5KB)
----vendor()
--------assets()
----config()
--------cable.yml(116B)
--------spring.rb(111B)
--------boot.rb(128B)
--------secrets.yml(965B)
--------database.yml(552B)
--------routes.rb(225B)
--------environments()
--------environment.rb(128B)
--------application.rb(486B)
--------locales()
--------puma.rb(2KB)
--------initializers()
----lib()
--------assets()
--------tasks()
----Gemfile(1KB)
----.learn(68B)
----Rakefile(249B)
----Gemfile.lock(5KB)