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 ...
随机推荐
- Winscp使用sudo user登录
为了安全期间, 一般设置了禁止root用户ssh登录.使用普通用户登录后再sudo获取操作权限. 可为了文件传文件使用winscp,如何使用sudo登录以获取权限传文件呢 先在服务端设置sudo帐号权 ...
- 【C#】C#线程_混合线程的同步构造
目录结构: contents structure [+] 一个简单的混合锁 FCL中的混合锁 ManualResetEventSlim类和SemaphoreSlim类 Monitor类和同步块 Rea ...
- 【20170506】贝业新兄弟IT总监李济宏:第三方家居物流的IT架构探索
5月6日,物流人的节日,这一天,全国的物流人汇聚中国上海嘉定,以“新时代.新物流”为主题的2017中国货运产业大会暨货运企业家高峰论坛如期召开,同时,首届嘉年华活动隆重开启,八大主题分享活动,精彩进行 ...
- easyui的datagrid和treegrid的使用
$('#listTree').treegrid({ idField: 'id', treeField: 'menuName', columns: [[ { title: 'Task Name', fi ...
- EL表达式具体解释
在 JSP 页面中,使用标签库取代传统的 Java 片段语言来实现页面的显示逻辑已经不是新技术了,然而.由自己定义标签非常easy造成反复定义和非标准的实现.鉴于此.出现了 JSTL ( JSP St ...
- mtr命令详解诊断网络路由
首先安装mtr# yum -y install mtr 一般在windows 来判断网络连通性用ping 和tracert, ping的话可以来判断丢包率,tracert可以用来跟踪路由, 在 ...
- ajax-page局部刷新分页实例
1.引用文件:connect.php <?php $host="localhost"; $db_user="root"; $db_pass="r ...
- Spark--sql--所有函数举例(spark-2.x版本)
! expr - Logical not. % expr1 % expr2 - Returns the remainder afterexpr1/expr2. Examples: > SELEC ...
- 【PostgreSQL】资料索引(来源:德哥)
PostgreSQL 多应用场景实践 - 沙箱实验 https://github.com/digoal/blog/blob/master/201805/20180524_02.md 一.GIS < ...
- python使用requests发送application/x-www-form-urlencoded请求数据
def client_post_formurlencodeddata_requests(request_url,requestJSONdata): #功能说明:发送以form表单数据格式(它要求数据名 ...