2016-05-05 80 views
2

我有一個名爲Template的模型,該模型與名爲RelatedTemplate的連接模型有self referential has-many through關聯。唯一性驗證不起作用

Template模型是這樣寫的:

has_many :related_templates, :foreign_key => :parent_id 
has_many :children, :through => :related_templates, :source => :child 

has_many :inverse_related_templates, class_name: "RelatedTemplate", :foreign_key => :child_id 
has_many :parents, :through => :inverse_related_templates, :source => :parent 

validates :name, presence: true, uniqueness: { case_sensitive: false }, 
       length: { maximum: 100, too_long: "should be maximum %{count} characters" } 

的加入模型RelatedTemplate是:

belongs_to :parent, class_name: 'Template' 
belongs_to :child, class_name: 'Template' 

在控制檯當我試圖創建具有相同的名稱作爲一個子模板父母名稱,則驗證不起作用:

rails c --sandbox 
Loading development environment in sandbox (Rails 4.2.5) 
Any modifications you make will be rolled back on exit 
irb(main):001:0> a = Template.new 
=> #<Template id: nil, client_id: nil, something_id: nil, name: nil, description: nil, another_something_id: nil, video_id: nil, container_id: nil> 
irb(main):002:0> a.something_id=1 
=> 1 
irb(main):003:0> a.name="Hamza" 
=> "Hamza" 
irb(main):004:0> a.another_something_id=16 
=> 16 
irb(main):005:0> a.container_id=2 
=> 2 
irb(main):006:0> a.children.build(name: "Hamza", another_something_id: 16, container_id: 2) 
=> #<Template id: nil, client_id: nil, something_id: nil, name: "Hamza", description: nil, another_something_id: 16, video_id: nil, container_id: 2> 
irb(main):007:0> a.save 
(0.9ms) SAVEPOINT active_record_1 
Template Exists (1.3ms) SELECT 1 AS one FROM "templates" WHERE LOWER("templates"."name") = LOWER('Hamza') LIMIT 1 
Container Load (0.7ms) SELECT "containers".* FROM "containers" WHERE "containers"."id" = $1 LIMIT 1 [["id", 2]] 
Template Exists (0.5ms) SELECT 1 AS one FROM "templates" WHERE LOWER("templates"."name") = LOWER('Hamza') LIMIT 1 
Container Load (0.2ms) SELECT "containers".* FROM "containers" WHERE "containers"."id" = $1 LIMIT 1 [["id", 2]] 
SQL (0.3ms) INSERT INTO "templates" ("something_id", "name", "another_something_id", "container_id") VALUES ($1, $2, $3, $4) RETURNING "id" [["something_id", 1], ["name", "Hamza"], ["another_something_id", 16], ["container_id", 2]] 
SQL (0.2ms) INSERT INTO "templates" ("name", "another_something_id", "container_id") VALUES ($1, $2, $3) RETURNING "id" [["name", "Hamza"], ["another_something_id", 16], ["container_id", 2]] 
SQL (0.6ms) INSERT INTO "related_templates" ("parent_id", "child_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["parent_id", 156], ["child_id", 157], ["created_at", "2016-05-05 07:03:51.496346"], ["updated_at", "2016-05-05 07:03:51.496346"]] 
(0.2ms) RELEASE SAVEPOINT active_record_1 
=> true 

現在查詢:

irb(main):016:0> a.name 
=> "Hamza" 
irb(main):017:0> a.children.first.name 
Template Load (1.9ms) SELECT "templates".* FROM "templates" INNER JOIN "related_templates" ON "templates"."id" = "related_templates"."child_id" WHERE "related_templates"."parent_id" = $1 ORDER BY "templates"."id" ASC LIMIT 1 [["parent_id", 158]] 
=> "Hamza" 

不知道這裏有什麼問題嗎?

+0

將關鍵字添加到關係'has_many:related_templates,:foreign_key =>:parent_id,source =>:template' –

+0

我做了,但它不起作用 –

+0

怎麼不工作? –

回答

4

當你驗證它們時,兩個對象都只存在於內存中。唯一性驗證在兩種情況下都會通過,因爲驗證會檢查數據庫中是否存在類似的記錄。

看一看日誌:

# you call save 
irb(main):007:0> a.save 
(0.9ms) SAVEPOINT active_record_1 

# Rails validates the first object 
Template Exists (1.3ms) SELECT 1 AS one FROM "templates" WHERE LOWER("templates"."name") = LOWER('Hamza') LIMIT 1 
Container Load (0.7ms) SELECT "containers".* FROM "containers" WHERE "containers"."id" = $1 LIMIT 1 [["id", 2]] 

# Rails validates the second object 
Template Exists (0.5ms) SELECT 1 AS one FROM "templates" WHERE LOWER("templates"."name") = LOWER('Hamza') LIMIT 1 
Container Load (0.2ms) SELECT "containers".* FROM "containers" WHERE "containers"."id" = $1 LIMIT 1 [["id", 2]] 

# At this point Rails thinks that both records will be fine, because the validation passed 

# Rails saved the first object 
SQL (0.3ms) INSERT INTO "templates" ("something_id", "name", "another_something_id", "container_id") VALUES ($1, $2, $3, $4) RETURNING "id" [["something_id", 1], ["name", "Hamza"], ["another_something_id", 16], ["container_id", 2]] 

# Rails saved the second object 
SQL (0.2ms) INSERT INTO "templates" ("name", "another_something_id", "container_id") VALUES ($1, $2, $3) RETURNING "id" [["name", "Hamza"], ["another_something_id", 16], ["container_id", 2]] 

這就是很好的例子,爲什麼Rails的唯一性驗證都沒有100%的安全。另一個例子可能是競爭條件,即多個請求同時觸發應用程序。

要避免此類問題,唯一的方法是向數據庫表添加唯一索引。

+0

我認爲你是對的。我將在db表中添加唯一性約束。可能那時我需要手動處理'數據庫約束錯誤'。對 ? –

+0

是的,你將不得不自己處理數據庫錯誤。 – spickermann