您现在的位置是:首页 > 电脑 > 

在rails中,如何轻松地反转与现有数据的一对一关系?(In rails, how to easily reverse a self

2025-07-18 02:43:55
在rails中,如何轻松地反转与现有数据的一对一关系?(In rails, how to easily reverse a self-referring one to one relation with existing data?) 我有一个已经拥有大量生产数据的rails项目。 我有一张桌子: node: id: integer follow_n
在rails中,如何轻松地反转与现有数据的一对一关系?(In rails, how to easily reverse a self-referring one to one relation with existing data?)

我有一个已经拥有大量生产数据的rails项目。 我有一张桌子:

node: id: integer follow_node_id: integer

每个follow_node都有一个parent_node,因此这是一个自我引用的一对一关系。 作为rails的新手,我错误地定义了这样的关系:

has_one :follow_node, class_name: 'ode', foreign_key: 'follow_node_id' belongs_to :parent_node, class_name: 'ode', foreign_key: 'follow_node_id'

错误很明显。 外键定义belongs_to关系,即follow_node_id指向parent_node而不指向follow_node 。

我想改变这种行为,以便它再次有意义。 我想要最终得到的关系是这样的:

has_one :follow_node, class_name: 'ode', foreign_key: 'follow_node_id' belongs_to :parent_node, class_name: 'ode', foreign_key: 'follow_node_id'

唯一的问题是:我收集了大量数据,并且大多数记录都设置了follow_node_id。 什么是更新所有数据的有效方法?

I have a rails project which already has a lot of production data. I have a certain table:

node: id: integer follow_node_id: integer

Every follow_node has a single parent_node, so this is a self-referring one-to-one relation. Being fairly new to rails, I mistakenly defined the relation like this:

has_one :follow_node, class_name: 'ode', foreign_key: 'follow_node_id' belongs_to :parent_node, class_name: 'ode', foreign_key: 'follow_node_id'

The mistake is obvious. The foreign key defines the belongs_to relation, the follow_node_id points to the parent_node and not to the follow_node.

I want to change this behaviour so that it makes sense again. the relation that I want to end up with is this one:

has_one :follow_node, class_name: 'ode', foreign_key: 'follow_node_id' belongs_to :parent_node, class_name: 'ode', foreign_key: 'follow_node_id'

The only problem is: I got loads of data, and most of the records have their follow_node_id set. What is an efficient way to update all the data?

最满意答案

我认为脚本看起来像:

ode.where(follow_node_id: nil).each do |node| parent_node = ode.where(follow_node_id: node.id).first node.update_attributes(follow_node_id: parent_node.id) parent_node.update_attributes(follow_node_id: nil) end

I'll do it like this.

Change the source code of the ode model file as described in the question. (The relation now works in incorrect direction, meaning that node.follow_node returns its parent node, and node.parent_node returns its follow node.)

Write a migration that does the following:

Create a new integer field (e.g. tmp_follow_node_id) For each ode record, store the ID of its parent_node in tmp_follow_node_id. In code: ode.{|n|n.update_attribute :tmp_follow_node_id, n.parent_node.id} Drop the follow_node_id field Rename the tmp_follow_node_id to follow_node_id Migrate

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/61871.html

相关标签:无
上传时间: 2023-04-21 00:31:30
留言与评论(共有 11 条评论)
本站网友 男人之痛
23分钟前 发表
nil).each do |node| parent_node = ode.where(follow_node_id
本站网友 强拍门
9分钟前 发表
n.parent_node.id} Drop the follow_node_id field Rename the tmp_follow_node_id to follow_node_id Migrate
本站网友 投资黄金白银
26分钟前 发表
什么是更新所有数据的有效方法? I have a rails project which already has a lot of production data. I have a certain table
本站网友 汇元网
24分钟前 发表
follow_node
本站网友 雪纺
0秒前 发表
has_one
本站网友 慢性肾衰
21分钟前 发表
foreign_key
本站网友 产能过剩
26分钟前 发表
id
本站网友 宾阳租房
0秒前 发表
id
本站网友 穿心莲
12分钟前 发表
'follow_node_id' 唯一的问题是:我收集了大量数据
本站网友 重庆发改委
13分钟前 发表
'follow_node_id' belongs_to