0

我想从 Rails 访问旧数据库模式。我有一个带有主键 OBJECT_ID 的表 NAGIOS_OBJECTS 和一个带有 HOST_OBJECT_ID 列的 NAGIOS_HOST_CHECKS 表。因此,我将关系定义如下:

class NagiosObject < ActiveRecord::Base
  has_one :nagios_host_check, :foreign_key => :host_object_id, :primary_key => :object_id
end

class NagiosHostCheck < ActiveRecord::Base
  belongs_to :nagios_object, :foreign_key => :host_object_id, :primary_key => :object_id
end

但是,当调用 a_nagios_object.nagios_host_check 或 a_nagios_host_check.nagios_object 时,我总是得到 nil。

知道我的代码有什么问题吗?

4

1 回答 1

2

foreign_key 和 primary_key 应该是字符串,而不是符号

前任:

class NagiosObject < ActiveRecord::Base
  has_one :nagios_host_check, :foreign_key => 'host_object_id', :primary_key => 'object_id'
end

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001317

于 2010-07-05T18:01:05.553 回答