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
870
[Rails 开发入门课程] 第五节 Controllers & Views
daqing
0
110
[Rails 开发入门课程] 第四节 Routes
daqing
0
200
[Rails 开发入门课程] 第二节 Model
daqing
4
480
[Rails 开发入门课程] 第一节 Rails 运行环境
daqing
7
430
Other Decks in Programming
See All in Programming
なんでRustの環境構築してないのにRust製のツールが動くの? / Why Do Rust-Based Tools Run Without a Rust Environment?
ssssota
14
47k
O Que É e Como Funciona o PHP-FPM?
marcelgsantos
0
240
KoogではじめるAIエージェント開発
hiroaki404
1
230
オンデバイスAIとXcode
ryodeveloper
0
370
Blazing Fast UI Development with Compose Hot Reload (droidcon London 2025)
zsmb
0
440
One Enishi After Another
snoozer05
PRO
0
170
contribution to astral-sh/uv
shunsock
0
570
Developer Joy - The New Paradigm
hollycummins
1
400
釣り地図SNSにおける有料機能の実装
nokonoko1203
0
200
퇴근 후 1억이 거래되는 서비스 만들기 | 내가 AI를 사용하는 방법
maryang
2
230
NIKKEI Tech Talk#38
cipepser
0
350
CSC305 Lecture 10
javiergs
PRO
0
330
Featured
See All Featured
Build The Right Thing And Hit Your Dates
maggiecrowley
38
2.9k
Imperfection Machines: The Place of Print at Facebook
scottboms
269
13k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.2k
Balancing Empowerment & Direction
lara
5
710
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
10
900
What's in a price? How to price your products and services
michaelherold
246
12k
[RailsConf 2023] Rails as a piece of cake
palkan
57
6k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.2k
BBQ
matthewcrist
89
9.9k
It's Worth the Effort
3n
187
28k
A Modern Web Designer's Workflow
chriscoyier
697
190k
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⽇日星期四