【发布时间】:2018-02-06 16:36:04
【问题描述】:
我的 Rails 应用程序中有两个表:Category 和 Service。我还在它们之间创建了一个关联表CategoriesService,除了验证CategoriesService 表上的ID 之外,一切正常——我只是注意到我能够创建与不存在的记录的关联。我想知道如何才能正确修复它——我怀疑 Rails 应该让我们创建一些数据库级别的验证,这可能会更快更干净。我是这样定义我的模型的:
class Category < ApplicationRecord
has_and_belongs_to_many :services
end
class Service < ApplicationRecord
has_and_belongs_to_many :categories
end
class CategoriesService < ApplicationRecord
end
我在想创建has_and_belongs_to_many 关系可以确保这种验证本身,但我知道我错了。我该如何解决这个问题?
【问题讨论】:
标签: ruby-on-rails validation many-to-many