前言:
在学习model关联之前,首先要牢记一下几点:
1.关联关系,两端都要写好,否则会出现初学者看不懂的错误。而且对于理解代码,非常有好处。
2.model的名字是单数,controller是复数。
3.blong_to后面必须是单数,而且必须是小写。has_many后面必须是复数。
一:一对多
例如:
王妈妈有两个孩子,小明和小亮。可以说,王妈妈,有多个孩子。也可以说:小明,有一个妈妈;小王,有一个妈妈。我们一般在设计表的时候,是这样设计的:
mothers表中id和name
sons表中有id和name
为了增加逻辑关系,主外键关系,会在多的一方,增加一列,所以sons表中有三列,id和name和mother_id(对应了mothers表的id)
普通SQL:
select test_associate.mothers.name from test_associate.mothers inner join test_associate.sons on sons.mother_id = mothers.id where sons.name = '小李'
ruby代码:
class Mother has_many :sons end class Son belongs_to :mother end
解释:一个妈妈又多个孩子,一个儿子属于一个妈妈。
我们在rails console可以测试下:
xiao_wang = Son.first
mom = xiaowang.mother
这个 .mother 方法就是由 class Son的belongs_to :mother这句话生成的。
也就是相当于转换成了一下的sql语句:
select * from mothers join sons on sons.mother_id = mothers.id where sons.id = 1
详细解释:
A:belongs_to :mother
B:belongs_to :mother, :class => 'Mother', :foreign_key => 'mother_id'
A=B
这个就是Rails最典型的根据惯例来编程,声明哪个表对应的是哪个class,再在class之间声明好关联关系。
1.belongs_to :mother, rails就能判断出: mothers 表,是一的那一端。 而当前class 是: "class Son", 那么rails 就知道了 两个表的对应关系。
2.:class => 'Mother', 表示, 一的那一端, 对应的model class是Mother. 根据rails的惯例, Mother model对应的是 数据库中的 mothers 表。
3.:foreign_key => 'mother_id', rails就知道了, 外键是 'mother_id'. 而一对多关系中, 外键是保存在 多的那一端(也就是 sons, 所以说,在 sons表中, 必须有一个列, 叫做: mother_id )
所以, 这个复杂的SQL 条件就齐备了, 可以生成了。
上面的ruby代码,配置好之后, 就可以这样调用:
son = Son.first son.mother # .mother方法, 是由 class Son 中的 belongs_to 产生的。 mother = Mother.first mother.sons # .sons 方法, 是由 class Mother 中的 hash_many 产生的。
二:一对一,比较简单,也不常用,这里不介绍。(老公和老婆)
三:多对多
例如:
一个学生,有多个老师,(学习了多门课程)
一个老师,可以教多个孩子(教一门课程,但是有好多学生来听这个课程)
我们往往会这样做:
students有id和name两个字段
teachers有id和name两个字段
放在任何一个表中都不合适,这是我们需要一张中间表,也就是桥梁表。
lessons有id和name和student_id和teacher_id
原始SQL:
select teachers.*, students.*, lessons.* from lessons from teachers , join teachers on lessons.teacher_id = teachers.id join students on lessons.student_id = students.id where students.name = '小王'
Ruby代码:
class Student has_many :lessons has_many :teachers, :through => :lessons end
提示:has_many :teachers, :through => :lessons 相当于
has_many :teachers, :class => 'Teacher', :foreign_key => 'teacher_id', :throught => :lessons
class Teachers
has_many :lessons
has_many :students, :through => :lessons
end
查看小王的老师有哪些,同上面的原始SQL语句。
Student.find_by_name('小王').teachers
以上就是本文给大家分享的全部内容了,给出的示例也非常的简单易懂,希望大家能够喜欢。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
- 炉石传说移动端商店打不开怎么办 移动端商店打不开解决方法分享
- 王菲《我的世界2》日本唱片志限量编号10CD[WAV+CUE]
- 乌兰托娅《爱从草原来HQII》头版限量编号[低速原抓WAV+CUE]
- 魏妙如.2017-《伟大的旅行》[WAV分轨]
- 科乐美送短跑冠军牌佬豪华礼包:顶罕卡片、决斗盘等
- 重大进展!PS4模拟器运行《重力眩晕:重制版》演示
- 南半球爱好者的福利 日本御姐级樱花妹高挑性感
- 玩家热议:哪些游戏通关后才算正式开始?
- 知名博主称赞《影之刃零》:有望成为现代最强之一?
- 泳衣快兜不住了!《莱莎工房》莱莎琳新手版开放预订
- 5.1版本「绘想游迹」全新游迹特效展示
- 原神纳塔篇OST&四周年分享H5正式上线
- 《原神》「星路拾忆」网页活动壁纸展示
- 群星.2024-祝你幸福!电影原声带【阿里巴巴】【FLAC分轨】
- 于台烟.2004-执着【乾坤唱片】【WAV+CUE】