我正在创建一个应用程序,其中有三个主要模型和它们之间的关系,我需要跟踪一些属性:
create_table "entities", force: true do |t|
t.string "name"
t.string "label"
t.string "img"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "entities", ["name"], name: "index_entities_on_name", unique: true, using: :btree
create_table "entity_group_relationships", force: true do |t|
t.integer "entity_id"
t.integer "group_id"
t.integer "order"
t.string "label"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "entity_group_relationships", ["entity_id", "group_id"], name: "index_entity_group_relationships_on_entity_id_and_group_id", unique: true, using: :btree
create_table "entity_property_relationships", force: true do |t|
t.integer "entity_id"
t.integer "property_id"
t.integer "group_id"
t.integer "order"
t.string "label"
t.string "value"
t.integer "visibility"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "group_property_relationships", force: true do |t|
t.integer "group_id"
t.integer "property_id"
t.integer "order"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "group_property_relationships", ["group_id", "property_id"], name: "index_group_property_relationships_on_group_id_and_property_id", unique: true, using: :btree
add_index "group_property_relationships", ["group_id"], name: "index_group_property_relationships_on_group_id", using: :btree
add_index "group_property_relationships", ["property_id"], name: "index_group_property_relationships_on_property_id", using: :btree
create_table "groups", force: true do |t|
t.string "name"
t.string "default_label"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "groups", ["name"], name: "index_groups_on_name", unique: true, using: :btree
create_table "properties", force: true do |t|
t.string "name"
t.string "units"
t.string "units_short"
t.string "default_label"
t.string "default_value"
t.integer "default_visibility"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "properties", ["name"], name: "index_properties_on_name", unique: true, using: :btree
class Entity < ActiveRecord::Base
has_many :entity_property_relationships
has_many :entity_group_relationships
has_many :properties,
through: :entity_property_relationships,
inverse_of: :entities
has_many :groups,
through: :entity_group_relationships,
inverse_of: :entities
validates :name,
presence: true
#rest omitted
end
class Property < ActiveRecord::Base
has_many :group_property_relationships
has_many :entity_property_relationships
has_many :entities,
through: :entity_property_relationships,
inverse_of: :properties
has_many :groups,
through: :group_property_relationships,
inverse_of: :properties
validates :name, presence: true
#rest omitted
end
class Group < ActiveRecord::Base
has_many :group_property_relationships
has_many :entity_group_relationships
has_many :entities,
through: :entity_group_relationships,
inverse_of: :groups
has_many :properties,
through: :group_property_relationships,
inverse_of: :groups
validates :name,
presence: true
#rest omitted
end
class EntityPropertyRelationship < ActiveRecord::Base
belongs_to :entity
belongs_to :property
validates :entity_id,
presence: true
validates :property_id,
presence: true
validates :order,
presence: :true
#rest omitted
end
class EntityGroupRelationship < ActiveRecord::Base
belongs_to :entity
belongs_to :group
validates :entity_id,
presence: true
validates :group_id,
presence: true
validates :order,
presence: true
#rest omitted
end
class GroupPropertyRelationship < ActiveRecord::Base
belongs_to :group
belongs_to :property
validates :group_id,
presence: true
validates :property_id,
presence: true
validates :order,
presence: true
#rest omitted
end
我要阻止的行为是,当property
被删除时,共享相同EntityPropertyRelationships
和Entity
的所有Group
也会被删除。这不是GroupPropertyRelationships
的情况,也不是EntityGroupRelationships
的情况,而且从我能看到和理解的所有内容来看,这些关系的设置都是相同的。
我正在用rspec测试这个,这里有一些测试。
it "should still own non-destroyed properties" do
@entity = Entity.create!(name: "entity")
@property1 = Property.create!(name: "property1")
@property2 = Property.create!(name: "property2")
@group = Group.create!(name: "group")
@group.own!(@property1)
@group.own!(@property2)
@entity.own!(@group)
@entity.utilizes?(@property1).should eq(true)
@entity.utilizes?(@property2).should eq(true)
@property1.destroy
@entity.utilizes?(@property1).should eq(false)
@entity.utilizes?(@property2).should eq(true)
@group.owns?(@property1).should eq(false)
@group.owns?(@property2).should eq(true)
end
测试1是失败的测试。 entity
仍应utilize
property
- utilizes?()
检查EntityPropertyRelationship
与给定{{1}之间是否存在entity
}}
property
it "should still own non-destroyed groups" do
@entity = Entity.create!(name: "entity")
@group1 = Group.create!(name: "group1")
@group2 = Group.create!(name: "group2")
@entity.own!(@group1)
@entity.own!(@group2)
@entity.owns?(@group1).should eq(true)
@entity.owns?(@group2).should eq(true)
@group1.destroy
@entity.owns?(@group1).should eq(false)
@entity.owns?(@group2).should eq(true)
end
测试2和测试3通过罚款。我似乎无法确定这些关系的内容,保证要求销毁每个 it "should still utilize non-destroyed groups' properties" do
@entity = Entity.create!(name: "entity")
@group1 = Group.create!(name: "group1")
@group2 = Group.create!(name: "group2")
@property1 = Property.create!(name: "property1")
@property2 = Property.create!(name: "property2")
@group1.own!(@property1)
@group2.own!(@property2)
@entity.own!(@group1)
@entity.own!(@group2)
@entity.owns?(@group1).should eq(true)
@entity.owns?(@group2).should eq(true)
@entity.utilizes?(@property1).should eq(true)
@entity.utilizes?(@property2).should eq(true)
@group1.destroy
@entity.owns?(@group1).should eq(false)
@entity.owns?(@group2).should eq(true)
@entity.utilizes?(@property1).should eq(false)
@entity.utilizes?(@property2).should eq(true)
end
,即使EntityPropertyRelationship
不是最初销毁的财产的ID。我是否需要改变关系,或者有什么方法可以通过destroy_callbacks来防止这种情况发生?
答案 0 :(得分:0)
哎哟!我刚刚发现了这个问题。我从没想过它会在哪里,这就是为什么花了这么长时间才找到它。在我的GroupPropertyRelationships
中,我有一个after_destroy
会破坏EntityPropertyRelationships
每个与Group's
相关联的Entities
。它应该只是破坏EntityPropertyRelationships
,其中group_id
和property_id
与被销毁的当前GroupPropertyRelationship
相同。我将after_destroy
的{{1}}代码替换为:
GroupPropertyRelationship
那一定是复制粘贴错误。