在我的网络应用中,我有节点和链接。链接有两个节点。一个节点是源节点,另一个节点是目标节点。基本上,我希望数据库中包含对节点的引用的源列和目标列。我想弄清楚如何实现这个。
以下是节点模型的迁移:
class CreateNodes < ActiveRecord::Migration
def change
create_table :nodes do |t|
t.string :name
t.integer :group
t.references :link, index: true
t.timestamps
end
end
end
这是节点模型:
class Nodes < ActiveRecord::Base
belongs_to :link
end
我正在尝试弄清楚如何为链接模型设置迁移。以下是我到目前为止的情况:
class CreateLinks < ActiveRecord::Migration
def change
create_table :links do |t|
t.integer :value
t.boolean :checked
t.timestamps
end
end
end
以下是我模特中的内容:
class Links < ActiveRecord::Base
has_many :nodes
end
正确的迁移会是这样的吗?
class CreateLinks < ActiveRecord::Migration
def change
create_table :links do |t|
t.integer :value
t.boolean :checked
t.references :source
t. references :target
t.timestamps
end
end
end
答案 0 :(得分:1)
t.references :smith
基本上是t.integer :smth_id
的快捷方式,所以如果您的节点属于Links,那么构造似乎是正确的。
不确定links#source
和links#target
指向的位置。