使用 Primary_key+foreign_key 选项关联的 Rails 问题
我想从 Rails 访问旧数据库模式。我有一张带有主键 OBJECT_ID 的表 NAGIOS_OBJECTS 和一张引用带有 HOST_OBJECT_ID 列的 NAGIOS_OBJECTS 的表 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。
知道我的代码有什么问题吗?
I want to access a legacy database schema from Rails. I have one table NAGIOS_OBJECTS with a primary key OBJECT_ID and one table NAGIOS_HOST_CHECKS that refers to NAGIOS_OBJECTS with a column HOST_OBJECT_ID. I thus defined the relations as follows:
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
However, when calling a_nagios_object.nagios_host_check or a_nagios_host_check.nagios_object, I always get nil.
Any idea what is wrong with my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
foreign_key 和 Primary_key 应该是字符串,而不是符号,
例如:
http://api .rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001317
foreign_key and primary_key should be strings, not symbols
ex:
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001317