Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails ActiveRecord外键设置为空_Ruby On Rails_Ruby_Activerecord - Fatal编程技术网

Ruby on rails ActiveRecord外键设置为空

Ruby on rails ActiveRecord外键设置为空,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我正在使用Ruby 1.8.7和Rails 2.3.8,我有以下根资源: class PointOfInterest < ActiveRecord::Base set_primary_key "Id" set_table_name "POI" has_many :attributes, :foreign_key => 'POIId' end 这是对根(PointOfInterest)和Multimedia记录的正确持久化。但是,属性未正确持久化。当感兴趣点的外键被正

我正在使用Ruby 1.8.7和Rails 2.3.8,我有以下根资源:

class PointOfInterest < ActiveRecord::Base
  set_primary_key "Id"
  set_table_name  "POI"

  has_many :attributes, :foreign_key => 'POIId'
end
这是对根(
PointOfInterest
)和
Multimedia
记录的正确持久化。但是,
属性
未正确持久化。当感兴趣点的外键被正确设置(
POID
)时,
Multimedia
记录的外键保持为
null

任何关于这一点的线索都非常感谢


谢谢

您的关系/外键被设置为交叉用途

如果
属性
有一个
多媒体
,则需要
多媒体
模型声明
属于:属性
,并且
多媒体
表包含
属性
的外键


我猜您不能更改数据库模式(否则,我不得不问您为什么使用非Rails标准表和键名);在这种情况下,您需要切换关系的意义。Make
属性属于:multimedia
,并且
多媒体有一个:属性
。然后,
属性
表中的
多媒体
FK指向正确的方向。

感谢您的快速响应!我试过了,但结果是一样的。
属性
表中的
POID
设置正确,但多媒体记录的ID(
属性
表中的
列)不正确。
class Attribute < ActiveRecord::Base
  set_primary_key "Id"
  set_table_name  "Attribute"

  belongs_to :point_of_interest, :foreign_key => 'POIId'

  has_one :multimedia, :foreign_key => 'Id', :primary_key => 'Value'
end
class Multimedia < ActiveRecord::Base
  set_primary_key "Id"
  set_table_name  "Multimedia"
end
poi = PointOfInterest.new
attr = poi.attributes.new
attr.SomeAttribute = 1
attr.build_multimedia(:content => 'test')
poi.save