0
我有与Product
和Document
模型通过has_many
/belongs_to
关系相关的Department
父模型。然后,Product
和Document
模型通过has_many :through
关系通过ProductDocuments
连接模型相互关联。确保父车型是相同的的has_many:通过协会轨3.2
作为关联的一部分,要求Product
和Document
模型在相互关联时具有相同的Department
父模型。
现在我做的是以下几点:
ProductDocumentsController < ApplicationController
...
def create
@product = Product.find(params[:product_document][:product_id])
@document = Document.find(params[:product_document][:document_id])
if @product.department.id == @document.department.id
...
end
end
...
end
这工作,但是感觉效率不高,因为它介绍了控制器配备了两个另外的数据库调用。
有没有人知道一种方式,这可以通过更有效的方式完成,也许通过模型中的验证?
任何帮助将不胜感激;谢谢!
“表示产品和文档模型在彼此关联时具有相同的产品父模型”。你的意思是“同一个部门”? – marvelousNinja
@marvelousNinja我确实,谢谢你指出。 – dSquared