Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
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
240
[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
850
[Rails 开发入门课程] 第五节 Controllers & Views
daqing
0
110
[Rails 开发入门课程] 第四节 Routes
daqing
0
200
[Rails 开发入门课程] 第二节 Model
daqing
4
470
[Rails 开发入门课程] 第一节 Rails 运行环境
daqing
7
430
Other Decks in Programming
See All in Programming
バッチを作らなきゃとなったときに考えること
irof
2
560
仕様変更に耐えるための"今の"DRY原則を考える
mkmk884
9
3.3k
クリーンアーキテクチャから見る依存の向きの大切さ
shimabox
5
1.2k
Datadog Workflow Automation で圧倒的価値提供
showwin
1
330
自力でTTSモデルを作った話
zgock999
0
130
若手バックエンドエンジニアが Elasticsearch を使ってみた話
hott0mott0
1
100
ABEMA iOS 大規模プロジェクトにおける段階的な技術刷新 / ABEMA iOS Technology Upgrade
akkyie
1
260
Ça bouge du côté des animations CSS !
goetter
2
170
15分で学ぶDuckDBの可愛い使い方 DuckDBの最近の更新
notrogue
3
870
Lambdaの監視、できてますか?Datadogを用いてLambdaを見守ろう
nealle
2
840
推しメソッドsource_locationのしくみを探る - はじめてRubyのコードを読んでみた
nobu09
2
370
運用しながらリアーキテクチャ
nealle
0
220
Featured
See All Featured
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
100
18k
Raft: Consensus for Rubyists
vanstee
137
6.8k
For a Future-Friendly Web
brad_frost
176
9.6k
Docker and Python
trallard
44
3.3k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
4
450
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
Visualization
eitanlees
146
15k
Done Done
chrislema
182
16k
Embracing the Ebb and Flow
colly
84
4.6k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
Designing on Purpose - Digital PM Summit 2013
jponch
117
7.1k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
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⽇日星期四