RoR - Expressing Database Relationships

One-to-One Association:
*一个person只对应一个personal_info
*一个personal_info只属于一个person
*belongs to用foreign key来链接
rails g model personal_info height:float weight:float person:references
此外你同时也有2个method
build_personal_info(hash)
#=> 在数据库中创建record
create_personal_info(hash)
#=> 在memory中创建record #=> Both remove the previous reference in the DB
用于person的实例
你需要用 has_one/belongs_to 去建立1对1association
One-to-Many Assoication:
*一个Person可能有1个或多个jobs
*但一个jobs只属于一个Person
belongs to的关系用foreign key来建立
rails g model job title company position_id person:references
#in model class Person < ActiveRecord::Base
has_one :personal_info
has_many :jobs
end class Job < ActiveRecord::Base
belongs_to :person
end
person.jobs = jobs
# 用新的array替换现有的jobs person.jobs << job(s)
# 添加新的jobs person.jobs.clear
# 将foreign key 设置为NULL, 解除这个person和jobs之间的关系 create! where!
#加了!才会提醒是否成功
# :dependent class Person < ActiveRecord::Base
has_one :personal_info, dependent: :destroy
end # 这样删除person的时候同时会删除跟person相关的personal_info
Many-to-Many association:
*一个person可以有很多hobbies
*一个hobbies可以被很多person共享
has_many&belongs_to_many
#join table rails g model hobby name rails g migration create_hobbies_people person:references hobby:references #生成Migration
class CreateHobbiesPeople < ActiveRecord::Migration
def change
create_table :hobbies_people, id: false do |t|
t.references :person, index: true, foreign_key: true
t.references :hobby, index:true, foreign_key: true
end
end
end #model
class Person < ActiveRecord::Base
has_and_belongs_to_many :hobbies
end class Hobby < ActiveRecord::Base
has_and_belongs_to_many :people
end
Many-to-Many 包含了2个model和3个migration
Join table只存在数据库中,不在ruby代码里面
Rich Many-to-Many assoiciation:
#SalaryRange Model and Migration rails g model salary_range min_salary:float max_salary:float job:references class CreateSalaryRanges < ActiveRecord::Migration
def change
create_table :salary_ranges do |t|
t.float :min_salary
t.float :max_salary
t.references :job, index: true, foreign_key: true t.timestamps null: false
end
end
end #model class Person < ActiveRecord::Base
has_many :jobs
has_many :approx_salaries, through: :jobs, source: :salary_range def max_salary
approx_salaries.maximum(:max_salary)
end
end class Job < ActiveRecord::Base
belongs_to :person
has_one :salary_range
end class SalaryRange < ActiveRecord::Base
belongs_to :job
end
RoR - Expressing Database Relationships的更多相关文章
- Object Relational Tutorial 对象关系教程
The SQLAlchemy Object Relational Mapper presents a method of associating user-defined Python classes ...
- 1.【使用EF Code-First方式和Fluent API来探讨EF中的关系】
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/relationship-in-entity-framework-using-code-firs ...
- 自连接<EntityFramework6.0>
自引用 public class PictureCategory { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ...
- laravel速记(笔记)
命令行: php artisan controller:make UserController This will generate the controller at /app/controller ...
- Writing your first Django app, part 1(转)
Let’s learn by example. Throughout this tutorial, we’ll walk you through the creation of a basic pol ...
- Object-Relational Structural Patterns
Single Table Inheritance Represents an inheritance hierarchy of classes as a single table that has c ...
- Laravel-nestedset that base left and right values tree package
This is a Laravel 4-5 package for working with trees in relational databases. Laravel 5.5, 5.6, 5.7, ...
- join 子句(C# 参考)
参考:https://msdn.microsoft.com/zh-cn/library/vstudio/bb311040%28v=vs.110%29.aspx 使用 join 子句可以将来自不同源序列 ...
- Writing your first Django
Quick install guide 1.1 Install Python, it works with Python2.6, 2.7, 3.2, 3.3. All these version ...
随机推荐
- (七):处理MFC
(一):简单介绍 为了可以在一个Winelib应用中使用MFC,你须要首先使用Winelib又一次编译MFC. 在理论上,你应该为Windows的MFC编写一个封装(怎样编写在后面介绍).可是,在实践 ...
- Mysql中NUMERIC和DECIMAL类型区别比较
decimal(numeric ) 同义,用于精确存储数值 . decimal 数据类型最多可存储 38 个数字,所有数字都能够放到小数点的右边.decimal 数据类型存储了一个准确(精确)的数字表 ...
- How the heck does async/await work in Python 3.5
https://snarky.ca/how-the-heck-does-async-await-work-in-python-3-5/
- 同一个tomcat下面放多个项目 每个项目用不同的域名访问
vim ./conf/server.conf <Host name=" appBase="/www/test1/webapps" ##这是war包存放的位置 unp ...
- 如何用jQuery获取选中行固定列的数据
[本文出自天外归云的博客园] 问题:把选中行的ID统计出来,组成一个数组传给后台(选中行的特点:class为danger) 办法如下: // 多选后点击下线按钮 $("#offline&qu ...
- Matlab信号处理基础
一. 简介 离散傅立叶.离散余弦和离散小波变换是图像.音频信号常用基础操作,时域信号转换到不同变换域以后,会导致不同程度的能量集中,信息隐藏利用这个原理在变换域选择适当位置系数进行修改,嵌入信息,并确 ...
- 【iCore4 双核心板_ARM】例程三十二:UART_IAP_ARM实验——更新升级STM32
实验现象及操作说明: 1.本例程共有两个代码包,APP和IAP,IAP程序功能实现将APP程序升级至STM32中. 2.直接上电或烧写程序将执行升级的APP应用程序. BIN升级文件产生方法: 1.编 ...
- 织梦dedecms移动版设置二级域名的方法 织梦如何设置m.开头的域名
dedecms/' target='_blank'>织梦dedecms建站系统自从2015.06.18号升级后,系统增加了最强的手机站功能,模板与PC模板分开,标签90%类似,数据同步,很牛很强 ...
- tomcat启动时非常慢,启动时 一直卡在Root WebApplicationContext: initialization completed(转)
转载地址:https://www.cnblogs.com/youxin/p/8793554.html 使用方式: JAVA_OPTS="-Djava.awt.headless=true -D ...
- js date 前一天
Date.prototype.Format = function (fmt) { var o = { "M+": this.getMonth() + 1, //月份 "d ...