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 ...
随机推荐
- redis应用-sortedset实现排行榜(转载)
package site.zy9.redisApp.test; import java.util.HashMap; import java.util.List; import java.util.Ma ...
- SpringBoot中自定义properties文件配置参数并带有输入提示
1. 创建配置类 在项目中创建一个参数映射类如下 @ConfigurationProperties(prefix = "user.info") public class MyPro ...
- [转]Docker中的镜像
引言 这篇文章中我们主要来探讨下Docker镜像,它是用来启动容器的构建基石,本文的所用到的Dcoker版本是17.1,API版本是1.33,Go的版本是1.9.2,OS是基于Arch Linux的M ...
- vim资源
1.http://vimcasts.org vim技巧,还有一个高达120美元的课程 目前,正在看http://vimcasts.org/blog/2013/02/habit-breaking-hab ...
- Vue自用axios封装
[本文出自天外归云的博客园] 这是我的Vue项目中的request.js文件,请求报错了看console就会有具体请求信息,方便调试.分享一下. 其中用到了axios和element-ui的组件,ax ...
- comake2
http://blog.csdn.net/lsjseu/article/details/23395565 comake允许用户通过编写COMAKE文件,来帮助用户管理编译依赖以及编译环境的开发工具: ...
- maven项目打包额外lib目录
maven项目依赖了几个额外的jar包一直都无法打进最终jar,不知道哪里出了问题.一直对这块不甚清楚,就大概梳理一下 默认打包方式: maven项目下,默认编译目录为 src/main/java和s ...
- EasyPermissions的流程
在app的build.gradle文件的dependencies中,添加依赖: implementation 'pub.devrel:easypermissions:1.3.0' import and ...
- 【Zookeeper系列】构建ZooKeeper应用(转)
原文地址:https://www.cnblogs.com/sunddenly/p/4064992.html 一.配置服务 配置服务是分布式应用所需要的基本服务之一,它使集群中的机器可以共享配置信息中那 ...
- [WCF] Restful 自定义宿主
IPersonRetriever: /* * 由SharpDevelop创建. * 用户: Administrator * 日期: 2017/6/2 * 时间: 22:13 * * 要改变这种模板请点 ...