如何对组合字段进行复杂的唯一性验证建模

6

一个链接有两个组件:componenta_idcomponentb_id。为此,在Link模型文件中我有:

belongs_to :componenta, class_name: "Component"
belongs_to :componentb, class_name: "Component"

validates :componenta_id, presence: true
validates :componentb_id, presence: true
validates :componenta_id, uniqueness: { scope: :componentb_id }
validates :componentb_id, uniqueness: { scope: :componenta_id }

在迁移文件中:

create_table :links do |t|
  t.integer  :componenta_id, null: false 
  t.integer  :componentb_id, null: false
  ...
end
add_index :links, :componenta_id
add_index :links, :componentb_id
add_index :links, [:componenta_id, :componentb_id], unique: true

问题:这一切都可以正常工作。现在我想让componantacomponentb的组合是唯一的,无论它们的顺序如何。所以无论哪个组件是componenta,哪个组件是componentb(毕竟这是同一个链接;两个相同组件之间的链接),这两个记录都不应该被允许,因为它们代表了相同的链接,因此不是唯一的:

  • componenta_id = 1 ; componentb_id = 2
  • componenta_id = 2 ; componentb_id = 1

我该如何创建这种唯一性验证?我已经使用以下代码实现了模型验证,但我想知道是否需要以及如何在迁移/数据库级别上添加验证…?


模型验证
我已经使用下面的代码实现了模型验证:

before_save :order_links
validates :componenta_id, uniqueness: { scope: :componentb_id }

private
  def order_links
    if componenta_id > componentb_id
      compb = componentb_id
      compa = componenta_id
      self.componenta_id = compb
      self.componentb_id = compa
    end
  end

以下测试确认了上述工作的有效性:
  1. test "combination of two links should be unique" do
  2.   assert @link1.valid?
  3.   assert @link2.valid?
  4.   @link1.componenta_id = 3     #@link2 already has combination 3-4
  5.   @link1.componentb_id = 4
  6.   assert_not @link1.valid?
  7.   @link1.componenta_id = 4
  8.   @link1.componentb_id = 3
  9.   assert_raises ActiveRecord::RecordNotUnique do
  10.    @link1.save
  11.  end
  12.end

迁移/数据库验证:
作为额外的安全级别,是否还有一种方法可以在数据库级别上进行验证?否则,仍然可以将以下两个记录都写入数据库:componenta_id = 1 ; componentb_id = 2componenta_id = 2 ; componentb_id = 1


这个对话中,建议创建多对多关系: has many :components validates_length_of :components,maximum:2 - skahlert
2个回答

4
也许可以通过以下方式控制链接的创建:
def create_unique_link( comp_1, comp_2 )
  if comp_1.id > comp_2.id
    first_component = comp_1
    second_component = comp_2
  end
  link = Link.find_or_create_by( componenta_id: first_comp.id, componentb_id: second_comp.id )
end

如果您需要验证,那么您可以自定义验证:

def ensure_uniqueness_of_link
  if comp_1.id > comp_2.id
    first_component = comp_1
    second_component = comp_2
  end

  if Link.where( componenta_id: first_component.id, componentb_id: second_component ).first
    errors.add( :link, 'Links should be unique' )
  end

end

但这只是模型验证,对吧?对于模型验证来说,我所拥有的代码已经可以工作了。这是在数据库级别上的验证,所以我的意思是,在迁移文件中还缺少这一部分。 - Nick
我无法想到在数据库层面上的通用方法。也许可以使用某种形式的数据库触发器来防止它?还可以有特定于数据库供应商的方法。 - roob

2
 validates :componenta_id, uniqueness: { scope: :componentb_id }
 validates :componentb_id, uniqueness: { scope: :componenta_id }

谢谢!但这样是否仍然允许1-2和2-1的组合。这意味着有一次组件ID为1的组件是componenta,组件ID为2的组件是componentb,另一次则相反。这就是我想要避免的。无论哪个是componenta,哪个是componentb,组合1-2都应该是唯一的。 - Nick
是的,它会,这就是为什么我本来要删除答案的原因;但我想除非它被投票否决,否则我会留下它 :D - Richard Peck
为什么不尝试上面的方法?它可能不是特别聪明,但可能会得到你需要的结果。 - Richard Peck
谢谢,我已经尝试过了。但不幸的是,添加到帖子中的测试在第9行失败了。 - Nick

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接