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的更多相关文章

  1. Object Relational Tutorial 对象关系教程

    The SQLAlchemy Object Relational Mapper presents a method of associating user-defined Python classes ...

  2. 1.【使用EF Code-First方式和Fluent API来探讨EF中的关系】

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/relationship-in-entity-framework-using-code-firs ...

  3. 自连接<EntityFramework6.0>

    自引用 public class PictureCategory { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ...

  4. laravel速记(笔记)

    命令行: php artisan controller:make UserController This will generate the controller at /app/controller ...

  5. 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 ...

  6. Object-Relational Structural Patterns

    Single Table Inheritance Represents an inheritance hierarchy of classes as a single table that has c ...

  7. 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, ...

  8. join 子句(C# 参考)

    参考:https://msdn.microsoft.com/zh-cn/library/vstudio/bb311040%28v=vs.110%29.aspx 使用 join 子句可以将来自不同源序列 ...

  9. 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 ...

随机推荐

  1. system generator学习笔记【01】

    作者:桂. 时间:2018-05-18  18:26:50 链接:http://www.cnblogs.com/xingshansi/p/9045914.html 前言 学习使用system gene ...

  2. Windows利用文件夹映射来同步文件

    在windows服务器上有时有这样的需求: 你的文件在f:\test中,但由于其它原因用户访问的是e:\test,如果又希望e:\test 中的文件与f:\test的保持同步,除了用同步软件来做外,可 ...

  3. 微信小程序--data的赋值与取值

    通过小程序官方文档可知:Page() 函数用来注册一个页面.接受一个 object 参数,其指定页面的初始数据.生命周期函数.事件处理函数等.其中的参数data用来设置初始数据,WXML 中的动态数据 ...

  4. 华为AR配置内部服务器示例(只有1个公网IP)

    AR配置公网和私网用户都可以通过公网地址访问内部服务器示例(只有1个公网IP) 适用于:V200R003C01及以后的系统软件版本. 组网需求: 由于只有1个公网IP(100.100.1.2),想实现 ...

  5. 基于HTML5 Tab选项卡动画切换特效

    基于HTML5 Tab选项卡动画切换特效.这是一款基于HTML5+CSS3实现的带有动画切换效果的Tab选项卡插件cbpFWTabs.效果图如下: 在线预览   源码下载 实现的代码. html代码: ...

  6. myeclipse 从数据库生成java实体类

  7. c++ 异常 warning: 'MEMORY_UNIT_NAME' defined but not used

    是开关的问题  , 将 #-g -O2 -pipe -W -Wall -Werror -fPIC -Wno-deprecated    c++ 去掉.不检查.

  8. 通过poi的XSSF实现生成excel文件

    maven导入依赖jar包: <dependency> <groupId>org.apache.poi</groupId> <artifactId>po ...

  9. Serv-u 备份处理

    需求分析 从删库到跑路,大家听过很多,但如果没删备份的话,那只能说玩的不够彻底,不专业.系统管理员一个非常重要的职责就是定期备份,以便在系统出现故障的时候能够及时恢复.我们来看下如何备份Serv-u系 ...

  10. 将 Entity Framework、LINQ 和 Model-First 用于 Oracle 数据库

    目的 本教程介绍如何使用 Entity Framework.语言集成查询 (LINQ),以及如何对 Oracle 数据库使用 Model-First 以生成数据定义语言 (DDL) 脚本. 所需时间 ...