Active Record的奇怪行为(belongs_to)

时间:2011-07-11 05:52:34

标签: ruby-on-rails activerecord reference iterator belongs-to

我通过在迭代器之前添加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

0 个答案:

没有答案