我有关于外键的这个问题 我想表中的外键叫做the_zombie_id 但是当我去控制台加载数据。 the_zombie_id不存在为Rails的模型定义外键关系似乎不起作用
class Zombie < ActiveRecord::Base
attr_accessible :age, :bio, :name
has_one :brain, :foreign_key => 'the_zombie_id'
end
class Brain < ActiveRecord::Base
attr_accessible :flavor, :status, :zombie_id
belongs_to :zombie
end
1.9.3-p547 :034 > brain=Brain.new => #<Brain id: nil, zombie_id: nil, status: nil, flavor: nil, created_at: nil, updated_at: nil>
> 1.9.3-p547 :035 > brain.the_zombie_id NoMethodError: undefined method `the_zombie_id' for #<Brain:0xaa0193c>
编辑后,我看到你的表中有一个zombie_id(尊重约定)......为什么不使用它?只需删除两个模型中的foreign_key定义并正常使用它。 – Fer 2014-10-07 14:58:57