2016-05-05 32 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