濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > ruby on rails中Model的關(guān)聯(lián)詳解

ruby on rails中Model的關(guān)聯(lián)詳解

熱門(mén)標(biāo)簽:北京語(yǔ)音電銷(xiāo)機(jī)器人價(jià)格 買(mǎi)了外呼系統(tǒng)不想用了怎么辦 開(kāi)封百應(yīng)電銷(xiāo)機(jī)器人聯(lián)系方式 浦東上海400開(kāi)頭的電話申請(qǐng) 武漢呼叫中心外呼系統(tǒng)線路商 邯鄲外呼調(diào)研線路 樂(lè)昌電話機(jī)器人 真人語(yǔ)音電銷(xiāo)機(jī)器人系統(tǒng) 電話機(jī)器人電話卡封號(hào)怎么辦

前言:
在學(xué)習(xí)model關(guān)聯(lián)之前,首先要牢記一下幾點(diǎn):
1.關(guān)聯(lián)關(guān)系,兩端都要寫(xiě)好,否則會(huì)出現(xiàn)初學(xué)者看不懂的錯(cuò)誤。而且對(duì)于理解代碼,非常有好處。
2.model的名字是單數(shù),controller是復(fù)數(shù)。
3.blong_to后面必須是單數(shù),而且必須是小寫(xiě)。has_many后面必須是復(fù)數(shù)。

一:一對(duì)多

例如:
王媽媽有兩個(gè)孩子,小明和小亮??梢哉f(shuō),王媽媽,有多個(gè)孩子。也可以說(shuō):小明,有一個(gè)媽媽;小王,有一個(gè)媽媽。我們一般在設(shè)計(jì)表的時(shí)候,是這樣設(shè)計(jì)的:
mothers表中id和name
sons表中有id和name
為了增加邏輯關(guān)系,主外鍵關(guān)系,會(huì)在多的一方,增加一列,所以sons表中有三列,id和name和mother_id(對(duì)應(yīng)了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

解釋?zhuān)阂粋€(gè)媽媽又多個(gè)孩子,一個(gè)兒子屬于一個(gè)媽媽。
我們?cè)趓ails console可以測(cè)試下:
xiao_wang = Son.first 
mom = xiaowang.mother

這個(gè) .mother 方法就是由 class Son的belongs_to :mother這句話生成的。
也就是相當(dāng)于轉(zhuǎn)換成了一下的sql語(yǔ)句:

select * from mothers 
  join sons 
  on sons.mother_id = mothers.id 
  where sons.id = 1

詳細(xì)解釋?zhuān)?br />

A:belongs_to :mother
B:belongs_to :mother, :class => 'Mother', :foreign_key => 'mother_id'
A=B

這個(gè)就是Rails最典型的根據(jù)慣例來(lái)編程,聲明哪個(gè)表對(duì)應(yīng)的是哪個(gè)class,再在class之間聲明好關(guān)聯(lián)關(guān)系。
1.belongs_to :mother, rails就能判斷出: mothers 表,是一的那一端。 而當(dāng)前class 是: "class Son", 那么rails 就知道了 兩個(gè)表的對(duì)應(yīng)關(guān)系。
2.:class => 'Mother', 表示, 一的那一端, 對(duì)應(yīng)的model class是Mother. 根據(jù)rails的慣例, Mother model對(duì)應(yīng)的是 數(shù)據(jù)庫(kù)中的 mothers 表。
3.:foreign_key => 'mother_id', rails就知道了, 外鍵是 'mother_id'. 而一對(duì)多關(guān)系中, 外鍵是保存在 多的那一端(也就是 sons, 所以說(shuō),在 sons表中, 必須有一個(gè)列, 叫做: mother_id )
所以, 這個(gè)復(fù)雜的SQL 條件就齊備了, 可以生成了。
上面的ruby代碼,配置好之后, 就可以這樣調(diào)用:

son = Son.first
son.mother # .mother方法, 是由 class Son 中的 belongs_to 產(chǎn)生的。
mother = Mother.first
mother.sons  # .sons 方法, 是由 class Mother 中的 hash_many 產(chǎn)生的。

二:一對(duì)一,比較簡(jiǎn)單,也不常用,這里不介紹。(老公和老婆)

三:多對(duì)多

例如:
一個(gè)學(xué)生,有多個(gè)老師,(學(xué)習(xí)了多門(mén)課程)
一個(gè)老師,可以教多個(gè)孩子(教一門(mén)課程,但是有好多學(xué)生來(lái)聽(tīng)這個(gè)課程)
我們往往會(huì)這樣做:
students有id和name兩個(gè)字段
teachers有id和name兩個(gè)字段
放在任何一個(gè)表中都不合適,這是我們需要一張中間表,也就是橋梁表。
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 相當(dāng)于
has_many :teachers, :class => 'Teacher', :foreign_key => 'teacher_id', :throught => :lessons
class Teachers 
  has_many :lessons 
  has_many :students, :through => :lessons 
end

查看小王的老師有哪些,同上面的原始SQL語(yǔ)句。

Student.find_by_name('小王').teachers

以上就是本文給大家分享的全部?jī)?nèi)容了,給出的示例也非常的簡(jiǎn)單易懂,希望大家能夠喜歡。

您可能感興趣的文章:
  • Windows下Ruby on Rails開(kāi)發(fā)環(huán)境安裝配置圖文教程
  • ruby on rails 代碼技巧
  • 攻克CakePHP(PHP中的Ruby On Rails框架)圖文介紹
  • 在阿里云 (aliyun) 服務(wù)器上搭建Ruby On Rails環(huán)境
  • 在Ruby on Rails中使用AJAX的教程
  • CentOS中配置Ruby on Rails環(huán)境
  • win7安裝ruby on rails開(kāi)發(fā)環(huán)境

標(biāo)簽:鄂州 六安 宜春 松原 自貢 淄博 河北 石嘴山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ruby on rails中Model的關(guān)聯(lián)詳解》,本文關(guān)鍵詞  ruby,rails,中,Model,的,關(guān)聯(lián),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《ruby on rails中Model的關(guān)聯(lián)詳解》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于ruby on rails中Model的關(guān)聯(lián)詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    拉孜县| 固安县| 北宁市| 阿尔山市| 元朗区| 平果县| 贡觉县| 耿马| 瑞昌市| 广西| 湘西| 吉木萨尔县| 新田县| 方山县| 泰和县| 平和县| 贺州市| 本溪| 康定县| 廊坊市| 楚雄市| 昌吉市| 宾川县| 怀柔区| 读书| 固阳县| 祥云县| 绍兴市| 金湖县| 九江市| 万山特区| 岚皋县| 拉萨市| 民丰县| 兰考县| 本溪市| 县级市| 偃师市| 青州市| 芜湖县| 武乡县|