我试图实现与外键的has_many
belongs_to
关系。使用foreing_key时找不到ID没有ID的记录
模板
belongs_to :project
项目
has_many :templates
belongs_to :coordinator, foreign_key: :coordinator_id, class_name: 'Person'
人
has_many :projects
我能够做
template = Template.new
template.project = Project.new
template.project # my empty project is showed
但这不起作用
project = Project.new
project.coordinator = Person.new # Everything is ok until here
project.coordinator # ActiveRecord::RecordNotFound: Couldn't find Person without an ID
我不知道我做错了,也许我需要指定主键在某处,但我无法弄清楚。
我使用Rails 4
尝试从项目的'belongs_to'字段中删除'foreign_key :: coordinator_id',尝试将has_many:projects项目更改为has_many:projects :: as =>:coordinator' –
。 –
这些解决方案都不起作用。 – Wagner