RoR - Expressing Database Relationships
2024-09-28 05:14:05
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
最新文章
- VC++ : error LNK2001: unresolved external symbol ";__declspec(dllimport) public: __thiscall std::basic_string<;wchar_t,struct std::char_traits<;wchar_t>;
- Django 1.7 throws django.core.exceptions.AppRegistryNotReady: Models aren&#39;t loaded yet
- ora-01400 无法将NULL插入 ID 解决方法
- fasta文件拆分与合并
- 0x1L
- Tornado 的教材
- Eclipse Velocity插件安装
- C#编程建言笔记
- Docker笔记二:Lumen &; Redis
- Android之PhotoView使用
- Jenkins高级用法 - Jenkinsfile 介绍及实战经验
- layui弹窗 之 iframe关闭
- Up-to-date cache with EclipseLink and Oracle
- interface 界面&;接口
- 【Mybatis】1、Mybatis拦截器学习资料汇总
- 质量不合格_区分excel两列不同的值
- Android 工程中各种文件的介绍
- Android Studio 1.1.0 向导页(首页) 解析,以及版本控制 (SVN 和 GIT 的检出)
- jar依赖
- Js如何调用本地应用程序
热门文章
- Java远程调用原理DEMO
- Effective Java 第三版——71. 避免不必要地使用检查异常
- 【谷歌浏览器】【谷歌地球】【Adobe 软件】离线安装包的下载地址
- JSON.stringify转化报错
- firewall防火墙使用
- MySQL: Set user variable from result of query
- 火车头采集器如何采集QQ群成员中的QQ号
- Nginx+Keepalived+Tomcat高可用负载均衡,Zookeeper集群配置,Mysql(MariaDB)搭建,Redis安装,FTP配置
- Ubuntu下创建新用户后,不能使用管理员用户下安装的Anaconda
- NuGet Packages are missing,This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.