惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Security @ Cisco Blogs
T
Threat Research - Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
F
Full Disclosure
博客园 - 【当耐特】
C
CERT Recently Published Vulnerability Notes
Engineering at Meta
Engineering at Meta
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Threatpost
I
Intezer
V2EX - 技术
V2EX - 技术
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
小众软件
小众软件
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
N
News | PayPal Newsroom
MyScale Blog
MyScale Blog
AI
AI
Vercel News
Vercel News
Spread Privacy
Spread Privacy
美团技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
Help Net Security
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 热门话题
U
Unit 42
L
LangChain Blog
Recent Announcements
Recent Announcements

博客园 - Cure

Oracle 查看哪个表被锁定,并获取对应的sessionID Yii框架常见问题汇总 Yii框架中安装srbac扩展方法 Yii框架中使用PHPExcel导出Excel文件 Yii框架中使用SRBAC作为权限管理模块时遇到的问题 文件名过长,无法删除的解决办法 Yii框架中使用bootstrap SilverLight中显示上标,下标,平方 10 Websites To Download Free HTML/CSS Templates 经过两天的努力,终于写出了第一个android的helloword sqlplus 帮助无法显示问题的解决 COBOL中USAGE的用法 asp.net mvc开发项目的部署?? 初试asp.net mvc感受 临时搞两天VC,在VC里如何获取当前程序的名字和路径以及如何分割字符串 Ruby on rails开发从头来(五十九)- ActiveRecord基础(预加载子记录) Borland以2300万美元卖掉CodeGear开发工具部门 下载安装了ubuntu 8.04,感觉很好很强大 项目管理工具Redmine + SubVersion + Apache + windows环境安装搭建
Ruby on rails开发从头来(五十八)- ActiveRecord基础(自关联)
Cure · 2008-05-07 · via 博客园 - Cure
 

或许存在这样的情况,在一个表中,一条记录关联到表中的另一条记录,例如,公司中的每个雇员都有上级和下级,而他们同时又是雇员,在Rails中你可以这样使用Employee类:

class Employee < ActiveRecord::Base

belongs_to :manager,

:class_name => "Employee",

:foreign_key => "manager_id"

belongs_to :mentor,

:class_name => "Employee",

:foreign_key => "mentor_id"

has_many :mentored_employees,

:class_name => "Employee",

:foreign_key => "mentor_id"

has_many :managed_employees,

:class_name => "Employee",

:foreign_key => "manager_id"

end

让我们使用一些数据,这里雇员ClemDawn都有上级和下级:

Employee.delete_all

adam = Employee.create(:id => 1, :name => "Adam")

beth = Employee.create(:id => 2, :name => "Beth")

clem = Employee.new(:name => "Clem")

clem.manager = adam

clem.mentor = beth

clem.save!

dawn = Employee.new(:name => "Dawn")

dawn.manager = adam

dawn.mentor = clem

dawn.save!

现在我们可以通过关联,来回答“X的下属是谁?”,“Y的上级是谁?”。

p adam.managed_employees.map {|e| e.name} # => [ "Clem", "Dawn" ]

p adam.mentored_employees # => []

p dawn.mentor.name # => "Clem"