Active Record 的奇怪行为(belongs_to)

发布于 2024-11-19 04:07:17 字数 2711 浏览 3 评论 0原文

我通过在迭代器之前添加 goto_create_object.reload 解决了这个问题。


以下是日志: 似乎使用索引并首先得到不同的输出。顺便说一句,字段 io_namespace 定义为:

belongs_to :io_namespace, :class_name => "IoNamespace", :foreign_key => "io_namespace"

(rdb:1) pp goto_create_object.io_fields[0]
ActiveRecord::AssociationTypeMismatch Exception: IoNamespace(#2160107980) expected, got String(#2148455420)
(rdb:1) pp goto_create_object.io_fields.first
 IoField io_uuid: "ac636fcc-ab80-11e0-8ef4-c82a14fffeca", io_active: false, io_application: nil, io_calculated: false, io_code: nil, io_created_by: "e6bf32c9-bdf0-33c8-8f4a-2390e174127d", io_created_on: "2011-07-11 13:43:15", io_datatype: "dc9a52a3-edbe-3937-9ef4-4bb24d076f77", io_deleted: false, io_description: nil, io_documentation: nil, io_folder: nil, io_icon: nil, io_identifier: "changed_to", io_indexed: false, io_localized: false, io_name: "Changed To", io_namespace: "97bc28f3-733f-3d26-8377-5718c4e2c544", io_notes: nil, io_owner: "e6bf32c9-bdf0-33c8-8f4a-2390e174127d", io_package: nil, io_private: false, io_related_object: nil, io_required: false, io_tags: nil, io_updated_by: nil, io_updated_on: nil>

它将给出正确的值。这是怎么发生的以及如何解决它。

这是模型:

class UUIDIORecord < UUIDRecord
  before_create :generate_default_fields

  #for namespace
  belongs_to :io_namespace, :class_name => "IoNamespace", :foreign_key => "io_namespace"

end

class IoObject < UUIDIORecord  
  set_table_name "io_object"

  #for fields
  has_many :rel_io_object_fields, :class_name => "RelIoObjectField", :foreign_key => "io_source_record", :inverse_of => :io_object, :dependent => :destroy
  has_many :io_fields, :class_name => "IoField", :through => :rel_io_object_fields, :dependent => :destroy

  #for relationship
  has_many :io_source_relationships, :class_name => "IoRelationShip", :foreign_key => "io_source_object", :inverse_of => :io_source_object, :dependent => :destroy

  # for primary name
  belongs_to :io_primary_name, :class_name => "IoField", :foreign_key => "io_primary_name"    
end

class IoField < UUIDIORecord
  set_table_name "io_field"

  #for object
  has_many :rel_io_object_fields, :class_name => "RelIoObjectField", :foreign_key => "io_target_record", :inverse_of => :io_field
  has_many :io_objects, :class_name => "IoObject", :through => :rel_io_object_fields

  #for datatype
  belongs_to :io_datatype, :class_name => "IoDataType", :foreign_key => "io_datatype"  

  # for relationship
  has_many :io_relationships, :class_name => "IoRelationShip", :foreign_key => "io_related_field"

  before_destroy :output_when_destroy
end

I resolve this by adding goto_create_object.reload before iterator.


Here is the log:
seems to use index and first get the different output. by the way, the field io_namespace is defined as:

belongs_to :io_namespace, :class_name => "IoNamespace", :foreign_key => "io_namespace"

(rdb:1) pp goto_create_object.io_fields[0]
ActiveRecord::AssociationTypeMismatch Exception: IoNamespace(#2160107980) expected, got String(#2148455420)
(rdb:1) pp goto_create_object.io_fields.first
 IoField io_uuid: "ac636fcc-ab80-11e0-8ef4-c82a14fffeca", io_active: false, io_application: nil, io_calculated: false, io_code: nil, io_created_by: "e6bf32c9-bdf0-33c8-8f4a-2390e174127d", io_created_on: "2011-07-11 13:43:15", io_datatype: "dc9a52a3-edbe-3937-9ef4-4bb24d076f77", io_deleted: false, io_description: nil, io_documentation: nil, io_folder: nil, io_icon: nil, io_identifier: "changed_to", io_indexed: false, io_localized: false, io_name: "Changed To", io_namespace: "97bc28f3-733f-3d26-8377-5718c4e2c544", io_notes: nil, io_owner: "e6bf32c9-bdf0-33c8-8f4a-2390e174127d", io_package: nil, io_private: false, io_related_object: nil, io_required: false, io_tags: nil, io_updated_by: nil, io_updated_on: nil>

it will give the correct value. how does that happen and how to solve it.

Here is the model:

class UUIDIORecord < UUIDRecord
  before_create :generate_default_fields

  #for namespace
  belongs_to :io_namespace, :class_name => "IoNamespace", :foreign_key => "io_namespace"

end

class IoObject < UUIDIORecord  
  set_table_name "io_object"

  #for fields
  has_many :rel_io_object_fields, :class_name => "RelIoObjectField", :foreign_key => "io_source_record", :inverse_of => :io_object, :dependent => :destroy
  has_many :io_fields, :class_name => "IoField", :through => :rel_io_object_fields, :dependent => :destroy

  #for relationship
  has_many :io_source_relationships, :class_name => "IoRelationShip", :foreign_key => "io_source_object", :inverse_of => :io_source_object, :dependent => :destroy

  # for primary name
  belongs_to :io_primary_name, :class_name => "IoField", :foreign_key => "io_primary_name"    
end

class IoField < UUIDIORecord
  set_table_name "io_field"

  #for object
  has_many :rel_io_object_fields, :class_name => "RelIoObjectField", :foreign_key => "io_target_record", :inverse_of => :io_field
  has_many :io_objects, :class_name => "IoObject", :through => :rel_io_object_fields

  #for datatype
  belongs_to :io_datatype, :class_name => "IoDataType", :foreign_key => "io_datatype"  

  # for relationship
  has_many :io_relationships, :class_name => "IoRelationShip", :foreign_key => "io_related_field"

  before_destroy :output_when_destroy
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文