Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
[Rails 开发入门课程] 第三节 ActiveRecord Association
Search
David Zhang
December 25, 2012
Programming
2
250
[Rails 开发入门课程] 第三节 ActiveRecord Association
这节主要介绍 Rails Model 之间的关系,比如 一对一,一对多和多对多。
David Zhang
December 25, 2012
Tweet
Share
More Decks by David Zhang
See All by David Zhang
Implement Heap Sort in Erlang
daqing
0
880
[Rails 开发入门课程] 第五节 Controllers & Views
daqing
0
110
[Rails 开发入门课程] 第四节 Routes
daqing
0
210
[Rails 开发入门课程] 第二节 Model
daqing
4
480
[Rails 开发入门课程] 第一节 Rails 运行环境
daqing
7
430
Other Decks in Programming
See All in Programming
HTTPプロトコル正しく理解していますか? 〜かわいい猫と共に学ぼう。ฅ^•ω•^ฅ ニャ〜
hekuchan
2
440
Context is King? 〜Verifiability時代とコンテキスト設計 / Beyond "Context is King"
rkaga
10
1.4k
新卒エンジニアのプルリクエスト with AI駆動
fukunaga2025
0
230
Basic Architectures
denyspoltorak
0
120
AIコーディングエージェント(NotebookLM)
kondai24
0
230
令和最新版Android Studioで化石デバイス向けアプリを作る
arkw
0
450
Kotlin Multiplatform Meetup - Compose Multiplatform 외부 의존성 아키텍처 설계부터 운영까지
wisemuji
0
130
Navigation 3: 적응형 UI를 위한 앱 탐색
fornewid
1
460
Full-Cycle Reactivity in Angular: SignalStore mit Signal Forms und Resources
manfredsteyer
PRO
0
170
著者と進める!『AIと個人開発したくなったらまずCursorで要件定義だ!』
yasunacoffee
0
160
Python札幌 LT資料
t3tra
7
1.1k
AtCoder Conference 2025「LLM時代のAHC」
imjk
2
590
Featured
See All Featured
The Cost Of JavaScript in 2023
addyosmani
55
9.4k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.8k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
1.8k
Code Review Best Practice
trishagee
74
19k
Deep Space Network (abreviated)
tonyrice
0
21
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.6k
Build your cross-platform service in a week with App Engine
jlugia
234
18k
Embracing the Ebb and Flow
colly
88
4.9k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
2
260
Done Done
chrislema
186
16k
Testing 201, or: Great Expectations
jmmastey
46
7.8k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.6k
Transcript
课前准备 https://github.com/daqing/rails-intro-forum/wiki/Preparing-for-the-course 12年12月27⽇日星期四
RAILS 开发⼊入⻔门课程 Introduction to Web development with Rails @daqing 12年12月27⽇日星期四
⺫⽬目标 理解 Rails 中的核⼼心概念 了解 Rails 项⺫⽬目的基本开发过程 可以开发简单的 Rails 应⽤用
12年12月27⽇日星期四
RAILS 开发⼊入⻔门课程 RAILS 运⾏行环境 Model Routes 第⼀一节 第⼆二节 第四节 Controllers
& Views 第五节 ActiveRecord Association 第六节 Form API 第三节 12年12月27⽇日星期四
内容回顾 浏览器 Web Server App Server DB Rails API HTTP
请求处理流程 12年12月27⽇日星期四
RAILS APP 核⼼心结构 DB request Model View Controller ActionPack Routes
12年12月27⽇日星期四
RAILS APP 核⼼心结构 DB request Model View Controller ActionPack Routes
12年12月27⽇日星期四
TOPIC id subject content 1 欢迎投票 2012 年度最受欢迎的中 国开源项⺫⽬目 2
现已⽀支持上传图⽚片 图⽚片存储在⼜又拍云 topics 12年12月27⽇日星期四
12年12月27⽇日星期四
MULTI-MODELS 12年12月27⽇日星期四
12年12月27⽇日星期四
12年12月27⽇日星期四
12年12月27⽇日星期四
12年12月27⽇日星期四
ACTIVERECORD ASSOCIATION 第三节 12年12月27⽇日星期四
id subject content 1 欢迎投票 2012 年度最受欢迎的中国开 源项⺫⽬目 2 现已⽀支持上传图⽚片
图⽚片存储在⼜又拍云 topics id nickname email 1 daqing
[email protected]
2 matz
[email protected]
users 12年12月27⽇日星期四
id subject content 1 欢迎投票 2012 年度最受欢迎的中国开 源项⺫⽬目 2 现已⽀支持上传图⽚片
图⽚片存储在⼜又拍云 topics id nickname email 1 daqing
[email protected]
2 matz
[email protected]
users 如何建⽴立关系? 12年12月27⽇日星期四
只需要了解⼀一个概念…… 12年12月27⽇日星期四
FOREIGN KEY 12年12月27⽇日星期四
FOREIGN KEY id subject content 1 欢迎投票 2012 年度最受欢迎的中国开 源项⺫⽬目
2 现已⽀支持上传图⽚片 图⽚片存储在⼜又拍云 topics id nickname email 1 daqing
[email protected]
2 matz
[email protected]
users 12年12月27⽇日星期四
FOREIGN KEY topics id nickname email 1 daqing
[email protected]
2
matz
[email protected]
users id user_id content subject 1 1 2012 年度最受欢迎 的中国开源项⺫⽬目 欢迎投票 2 2 图⽚片存储在⼜又拍云 现已⽀支持上传图⽚片 12年12月27⽇日星期四
FOREIGN KEY topics id nickname email 1 daqing
[email protected]
2
matz
[email protected]
users id user_id content subject 1 1 2012 年度最受欢迎 的中国开源项⺫⽬目 欢迎投票 2 2 图⽚片存储在⼜又拍云 现已⽀支持上传图⽚片 12年12月27⽇日星期四
FOREIGN KEY topics id nickname email 1 daqing
[email protected]
2
matz
[email protected]
users id user_id content subject 1 1 2012 年度最受欢迎 的中国开源项⺫⽬目 欢迎投票 2 2 图⽚片存储在⼜又拍云 现已⽀支持上传图⽚片 12年12月27⽇日星期四
FOREIGN KEY topics id nickname email 1 daqing
[email protected]
2
matz
[email protected]
users id user_id content subject 1 1 2012 年度最受欢迎 的中国开源项⺫⽬目 欢迎投票 2 2 图⽚片存储在⼜又拍云 现已⽀支持上传图⽚片 12年12月27⽇日星期四
FOREIGN KEY id subject content 1 欢迎投票 2012 年度最受欢迎的中国开 源项⺫⽬目
2 现已⽀支持上传图⽚片 图⽚片存储在⼜又拍云 topics id nickname email 1 daqing
[email protected]
2 matz
[email protected]
users 12年12月27⽇日星期四
FOREIGN KEY id subject content 1 欢迎投票 2012 年度最受欢迎的中国开 源项⺫⽬目
2 现已⽀支持上传图⽚片 图⽚片存储在⼜又拍云 topics id topic_id email nickname 1 1 daqing1986@gmail .com daqing 2 2
[email protected]
matz users 12年12月27⽇日星期四
FOREIGN KEY id subject content 1 欢迎投票 2012 年度最受欢迎的中国开 源项⺫⽬目
2 现已⽀支持上传图⽚片 图⽚片存储在⼜又拍云 topics id topic_id email nickname 1 1 daqing1986@gmail .com daqing 2 2
[email protected]
matz users 12年12月27⽇日星期四
FOREIGN KEY id subject content 1 欢迎投票 2012 年度最受欢迎的中国开 源项⺫⽬目
2 现已⽀支持上传图⽚片 图⽚片存储在⼜又拍云 topics id topic_id email nickname 1 1 daqing1986@gmail .com daqing 2 2
[email protected]
matz users 12年12月27⽇日星期四
FOREIGN KEY id subject content 1 欢迎投票 2012 年度最受欢迎的中国开 源项⺫⽬目
2 现已⽀支持上传图⽚片 图⽚片存储在⼜又拍云 topics id topic_id email nickname 1 1 daqing1986@gmail .com daqing 2 2
[email protected]
matz users 12年12月27⽇日星期四
12年12月27⽇日星期四
业务逻辑千变万化…… 12年12月27⽇日星期四
但是…… 12年12月27⽇日星期四
ASSOCIATIONS 1:1 1:M M:M A B A B B B
B B B A A A one to one one to many many to many 12年12月27⽇日星期四
ASSOCIATIONS 1:1 1:M M:M A B A B B B
B B B A A A one to one one to many many to many 12年12月27⽇日星期四
1:M A B B B 12年12月27⽇日星期四
1:M A B B B 12年12月27⽇日星期四
1:M id subject content 1 欢迎投票 2012 年度最受欢迎的中 国开源项⺫⽬目 2
现已⽀支持上传图⽚片 图⽚片存储在⼜又拍云 id topic_id content 1 1 已投 2 1 ⽀支持 3 2 这个功能不错 4 1 果断投票⽀支持 topics comments 12年12月27⽇日星期四
class Topic < ActiveRecord::Base end class Comment < ActiveRecord::Base end
has_many :comments :topic belongs_to 12年12月27⽇日星期四
DEMO 12年12月27⽇日星期四
ASSOCIATIONS 1:1 1:M M:M A B A B B B
B B B A A A one to one one to many many to many 12年12月27⽇日星期四
1:1 1:1 A B one to one 12年12月27⽇日星期四
1:1 id user_id weibo blog location phone qq 1 1
http:// weibo.com/ 1680956662 http:// rabelapp.com/ member/ daqing 北京 18612530152 420771712 2 1 http:// twitter.com/ yukihiro_matz http:// www.rubyist.n et/~matz/ 美国 3 9 ~ ~ ~ ~ ~ 4 87 ~ ~ ~ ~ ~ account id nickname email 1 daqing
[email protected]
2 matz
[email protected]
users 12年12月27⽇日星期四
class User < ActiveRecord::Base end class Account < ActiveRecord::Base end
has_one :account :user belongs_to 12年12月27⽇日星期四
DEMO 12年12月27⽇日星期四
ASSOCIATIONS 1:1 1:M M:M A B A B B B
B B B A A A one to one one to many many to many 12年12月27⽇日星期四
12年12月27⽇日星期四
12年12月27⽇日星期四
id subject content 1 欢迎投票 2012 年度最受欢迎的中国开 源项⺫⽬目 2 现已⽀支持上传图⽚片
图⽚片存储在⼜又拍云 topics id nickname email 1 daqing
[email protected]
2 matz
[email protected]
users id user_id topic_id 1 1 1 2 1 2 3 2 2 bookmark_mapping 12年12月27⽇日星期四
class User < ActiveRecord::Base end class Topic < ActiveRecord::Base end
has_many :bookmark_mappings :user belongs_to class BookmarkMapping < ActiveRecord::Base end belongs_to has_many :topics, :through => :bookmark_mappings :topic has_many :bookmark_mappings has_many :users, :through => :bookmark_mappings 12年12月27⽇日星期四
DEMO 12年12月27⽇日星期四
ASSOCIATIONS 1:1 1:M M:M A B A B B B
B B B A A A one to one one to many many to many ✓ ✓ ✓ 12年12月27⽇日星期四
ALL METHODS 1 has_one 2 has_one :through 3 has_many has_many
:through 4 has_and_belongs_to_many 5 belongs_to 6 12年12月27⽇日星期四
COMMON METHODS 1 has_one 2 has_one :through 3 has_many has_many
:through 4 has_and_belongs_to_many 5 belongs_to 6 12年12月27⽇日星期四
one more thing ... 12年12月27⽇日星期四
12年12月27⽇日星期四
12年12月27⽇日星期四
12年12月27⽇日星期四
12年12月27⽇日星期四
Movie c c c Book c c c 重复 ?
12年12月27⽇日星期四
Movie c c c Book 如果可以这样 ? c 12年12月27⽇日星期四
YES! 12年12月27⽇日星期四
POLYMORPHIC ASSOCIATION 12年12月27⽇日星期四
POLYMORPHIC ASSOCIATION id name 1900 牯岭街少年 杀⼈人事件 id commentable_type commentable_id
content 1 Movie 1900 经典电影,强烈推荐 2 Book 501 看不懂存在主义 id name 501 局外⼈人 movies books comments 12年12月27⽇日星期四
class Movie < ActiveRecord::Base end class < ActiveRecord::Base end has_many
:comments, :as => :commentable class Book < ActiveRecord::Base end has_many :comments, :as => :commentable belongs_to :commentable, :polymorphic => true Comment 12年12月27⽇日星期四
推荐阅读 http://guides.rubyonrails.org/association_basics.html Active Record Associations 12年12月27⽇日星期四
QUESTIONS ? 12年12月27⽇日星期四
谢谢 RAILS 开发⼊入⻔门课程 第三节 ACTIVERECORD ASSOCIATION(完) http://rabelapp.com https://github.com/daqing http://ruby-china.org/daqing 12年12月27⽇日星期四