$30 off During Our Annual Pro Sale. View Details »
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
エディターってAIで操作できるんだぜ
kis9a
0
740
AIエンジニアリングのご紹介 / Introduction to AI Engineering
rkaga
8
3k
C-Shared Buildで突破するAI Agent バックテストの壁
po3rin
0
390
「コードは上から下へ読むのが一番」と思った時に、思い出してほしい話
panda728
PRO
39
26k
UIデザインに役立つ 2025年の最新CSS / The Latest CSS for UI Design 2025
clockmaker
18
7.5k
生成AIを利用するだけでなく、投資できる組織へ
pospome
2
350
非同期処理の迷宮を抜ける: 初学者がつまづく構造的な原因
pd1xx
1
730
【Streamlit x Snowflake】データ基盤からアプリ開発・AI活用まで、すべてをSnowflake内で実現
ayumu_yamaguchi
1
120
AIコーディングエージェント(Gemini)
kondai24
0
240
AIの誤りが許されない業務システムにおいて“信頼されるAI” を目指す / building-trusted-ai-systems
yuya4
6
3.7k
複数人でのCLI/Infrastructure as Codeの暮らしを良くする
shmokmt
5
2.3k
AI 駆動開発ライフサイクル(AI-DLC):ソフトウェアエンジニアリングの再構築 / AI-DLC Introduction
kanamasa
3
160
Featured
See All Featured
Rebuilding a faster, lazier Slack
samanthasiow
85
9.3k
Product Roadmaps are Hard
iamctodd
PRO
55
12k
How to Think Like a Performance Engineer
csswizardry
28
2.4k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
132
19k
How GitHub (no longer) Works
holman
316
140k
Six Lessons from altMBA
skipperchong
29
4.1k
The World Runs on Bad Software
bkeepers
PRO
72
12k
Become a Pro
speakerdeck
PRO
31
5.7k
The Art of Programming - Codeland 2020
erikaheidi
56
14k
jQuery: Nuts, Bolts and Bling
dougneiner
65
8.3k
Reflections from 52 weeks, 52 projects
jeffersonlam
355
21k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
54k
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⽇日星期四