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
230
[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
790
[Rails 开发入门课程] 第五节 Controllers & Views
daqing
0
100
[Rails 开发入门课程] 第四节 Routes
daqing
0
170
[Rails 开发入门课程] 第二节 Model
daqing
4
460
[Rails 开发入门课程] 第一节 Rails 运行环境
daqing
7
410
Other Decks in Programming
See All in Programming
検証も兼ねて個人開発でHonoとかと向き合った話
hanetsuki
1
1.4k
Ruby Pattern Matching
bkuhlmann
0
940
Azure OpenAI Serviceのプロンプトエンジニアリング入門
tomokusaba
3
940
Apache Hive 4 on Treasure Data
ryukobayashi
1
450
Going beyond Apache Parquet's default settings
xhochy
0
140
Fragment Composition of GraphQL
quramy
13
1.6k
Snowflakeで眠ったデータを起こそう!
estie
0
160
Scalable Customer Journey Orchestration (CJO)
lewuathe
0
450
CDKコントリビュートの最初の壁を越えよう! -簡単issueの見つけ方-
badmintoncryer
3
360
Hanami and htmx
bkuhlmann
0
230
The Cutting Edge Of Versioning (LambdaConf 2024)
chriskrycho
0
230
Let's learn code review
riofujimon
2
620
Featured
See All Featured
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
8
3.4k
Become a Pro
speakerdeck
PRO
13
4.6k
How STYLIGHT went responsive
nonsquared
92
4.8k
For a Future-Friendly Web
brad_frost
172
9k
A better future with KSS
kneath
231
16k
Optimizing for Happiness
mojombo
370
69k
Git: the NoSQL Database
bkeepers
PRO
423
63k
Building Applications with DynamoDB
mza
88
5.6k
Debugging Ruby Performance
tmm1
70
11k
Visualization
eitanlees
137
14k
Practical Orchestrator
shlominoach
183
9.7k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
18
7k
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⽇日星期四