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 ...
随机推荐
- SNF软件开发机器人-子系统-功能-启用大按钮样式如何配置
启用大按钮 当启用大按钮被选中后,页面的按钮图表将以按钮配置中的大按钮样式显示. 1.效果展示: 2.使用说明: 打开显示页面,点击开发者选项的简单配置按钮.在功能表信息中选择启用大按钮复选框后保存.
- Mathmatica简介
作者:桂. 时间:2018-06-27 21:53:34 链接:https://www.cnblogs.com/xingshansi/p/9236502.html 前言 打算系统学习一些数学知识,容 ...
- MYSQL 优化器 源码解析
http://www.unofficialmysqlguide.com/introduction.html https://dev.mysql.com/doc/refman/8.0/en/explai ...
- 范型方法 & 范型参数 & 范型返回值
Java范型类 public class FanXingClassTest { public static void main(String args[]){ Test<Integer> ...
- Integer.parseInt vs Integer.valueOf
一直搞不清楚这两个有什么区别.刚才特意查了一下帖子. Integer.parseInt 返回的是 primitive int Integer.valueOf 返回的是 Integer Object ...
- perl控制流介绍(if条件,while,for循环,foreach)
1. 语句块:{ }之间的部分即为BLOCK语句块. 2. 条件语句:if ( expression ) BLOCK; if ( expression ) BLOCK1else BLOCK2 ...
- hdoj:2075
A|B? Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- nginx学习与使用
安装与运行 (从源码安装,这里OS为Ubuntu,参考资料:https://nginx.org/en/docs/configure.html) 1.下载并解压:https://nginx.org/en ...
- 【转】导致SQL执行慢的原因
索引对大数据的查询速度的提升是非常大的,Explain可以帮你分析SQL语句是否用到相关索引. 索引类似大学图书馆建书目索引,可以提高数据检索的效率,降低数据库的IO成本.MySQL在300万条记录左 ...
- 3.静态AOP实现-代理模式
通过代理模式实现在RegUser()方法本身业务前后加上一些自己的功能,如:BeforeProceed和AfterProceed,即不修改UserProcessor类又能增加新功能 定义1个用户接口, ...