【发布时间】:2019-07-17 11:15:20
【问题描述】:
我有一个表 Switches and Snapshots。一个 Switch 有很多 Snapshot,一个 Snapshot 属于一个 Switch。我无法通过关联创建新快照。在我的代码下方。
2.6.3 :014 > switch = Switch.new
=> #<Switch id: nil, switch: nil, ip_address: nil, created_at: nil, updated_at: nil>
2.6.3 :015 > switch.create_snapshot
NoMethodError (undefined method `create_snapshot' for #<Switch:0x00000000039950e0>)
应用程序/模型
class Switch < ApplicationRecord
has_many :snapshots
end
class Snapshot < ApplicationRecord
belongs_to :switch
end
psql 通过迁移配置
\d switches
Indexes:
"switches_pkey" PRIMARY KEY, btree (id)
Referenced by:
TABLE "snapshots" CONSTRAINT "fk_rails_5537742698" FOREIGN KEY (switch_id) REFERENCES switches(id)
# \d snapshots
Indexes:
"snapshots_pkey" PRIMARY KEY, btree (id)
"index_snapshots_on_switch_id" btree (switch_id)
Foreign-key constraints:
"fk_rails_5537742698" FOREIGN KEY (switch_id) REFERENCES switches(id)
【问题讨论】:
标签: ruby-on-rails activerecord associations