相关的数据库表具有以下模式:
sqlite> .schema structures
CREATE TABLE structures(
struct_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
batch_id INTEGER,
tag TEXT,
input_tag TEXT,
FOREIGN KEY (batch_id) REFERENCES batches(batch_id) DEFERRABLE INITIALLY DEFERRED);
sqlite> .schema residues
CREATE TABLE residues(
struct_id INTEGER NOT NULL,
resNum INTEGER NOT NULL,
name3 TEXT NOT NULL,
res_type TEXT NOT NULL,
FOREIGN KEY (struct_id) REFERENCES structures(struct_id) DEFERRABLE INITIALLY DEFERRED,
PRIMARY KEY (struct_id, resNum));
我有以下型号:
class Structure < ActiveRecord::Base
set_table_name "structures"
self.primary_key = "struct_id"
attr_accessible :struct_id, :batch_id, :input_tag
has_many :residues
end
class Residue < ActiveRecord::Base
self.primary_keys :struct_id, :resnum
belongs_to :structure, :foreign_key => 'struct_id'
attr_accessible :name3, :res_type, :resnum
end
在结构展示中我有:
<h2>Residues</h2>
<% @structure.residues.each do |residue| %>
<p>
<b>Residue number:</b>
<%= residue.resnum %>
</p>
<p>
<b>Residue type:</b>
<%= residue.res_type %>
</p>
<% end %>
但是,当我尝试显示结构时,我收到以下错误:
SQLite3::SQLException: no such column: residues.structure_id
为什么在数据库中查找structure_id而不是struct_id?看来我的外键没有得到尊重。
答案 0 :(得分:1)
您需要在关系的两边指定外键(has_many和belongs_to)