活动记录update_attribute执行对象(父)的关联对象(子)在DELETE查询时父母被更新 [英] Active record update_attribute executes a DELETE query on an associated object(child) of an object(parent) when parent is updated

查看:236
本文介绍了活动记录update_attribute执行对象(父)的关联对象(子)在DELETE查询时父母被更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的Rails:3.0.11

红宝石:1.9.3

为什么主动记录update_attribute对象(父),当我更新了家长的关联对象(子)上执行的DELETE查询?

以下是我的课:

 类用户的LT;的ActiveRecord :: Base的

  的has_many:user_keywords,:依赖=> :破坏
  的has_many:关键字:通过=> :user_keywords

结束

类UserKeyword<的ActiveRecord :: Base的
  belongs_to的:用户
  belongs_to的:关键字
结束

类关键词:LT;的ActiveRecord :: Base的
  的has_many:user_keywords
  的has_many:用户:通过=> :user_keywords
结束
 

在前端,我为每个关键字的复选框。

  • 复选框:关键字1
  • 复选框:关键字2
  • 复选框:关键字3
  • 复选框:关键字4

我第一次检查关键字1和关键字2复选框来更新用户的关键字。

我再次编辑通过检查关键字1和关键字3复选框,并更新了用户更新相关联的关键词的用户。 在我的USER_KEYWORDS表中的第二个更新,删除了previous记录。

以下是我的控制台输出,显示DELETE语句执行:

 用户负载(55.5ms)选择用户。*从用户WHERE用户,ID= 5 LIMIT 1
  关键字负荷(为0.2ms),选择关键词。* FROM关键字WHERE关键字,ID在(1,3)
  关键字负荷(为0.2ms),选择关键词。* FROM关键字INNER JOINuser_keywordsON关键词.ID =user_keywords.keyword_id WHERE((user_keywords.user_id = 5))
  AREL(为0.2ms)DELETE FROMuser_keywordsWHEREuser_keywords。user_ID的= 5,user_keywords。keyword_id= 2
  AREL(0.1毫秒)INSERT INTOuser_keywords(keyword_id,USER_ID,状态,created_at,的updated_at)VALUES(3,5,'F','2012-06-12 13:15 :43.912236','2012年6月12日13:15:43.912236')
 

本:对的has_many方法相关的选项是说(的http://guides.rubyonrails.org/association_basics.html#has_many-association-reference):

4.3.2.6:依赖

如果您设置:相关选项:摧毁,然后删除这个对象将调用销毁相关联的对象方法来删除这些对象。如果您设置:相关选项:DELETE_ALL,然后删除该对象将删除相应的对象,而不调用它们的destroy方法。如果您设置:相关选项:废除,然后删除该对象将设置外键关联对象为NULL

在使用此选项将被忽略:通过在关联选项​​。 ##

我无法理解这一点behavior.Can有人请详细说明或澄清这一点?

解决方案

delete语句只删除了该协会的关键字2.当你提交的两个复选框的形式检查,系统假定你想关联的这两个项目,但没有没有检查过其他人。因此,当它加载电流 user_keyword 的关联,它看到有一个现有的记录,你没有选择保留,所以它删除了。然后看到有一个新的,所以它创造了一个新的。

如果你想只添加的关系,而不是删除关系,那么我不会推荐复选框,如检查一个复选框意味着添加,但在取出支票意味着破坏关系(这是轨道在这种情况下所做的那样)。使用AJAX功能的按钮可能会更有意义。如果你想保持复选框,但不允许轨摧毁未选择的关系,那么你就必须建立自己的基于参数的关系,而不是依靠轨道魔术做的工作适合你。

请注意,这是不相关的:依赖选项,这仅仅是因为你已经设定了在模式和形式的关系。

Rails: 3.0.11

Ruby: 1.9.3

Why does active record update_attribute executes a DELETE query on an associated object(child) of an object(parent) when I updated the parent?

Following are my classes:

class User < ActiveRecord::Base

  has_many :user_keywords, :dependent => :destroy  
  has_many :keywords, :through => :user_keywords

end

class UserKeyword < ActiveRecord::Base
  belongs_to :user
  belongs_to :keyword
end

class Keyword < ActiveRecord::Base
  has_many :user_keywords
  has_many :users, :through => :user_keywords
end

On front-end I have a checkbox for each keyword.

  • Checkbox: Keyword 1
  • Checkbox: Keyword 2
  • Checkbox: Keyword 3
  • Checkbox: Keyword 4

First time I checked "Keyword 1" and "Keyword 2" checkboxes to update the user's keywords.

I again edited the user to update the associated keywords by checking "Keyword 1" and "Keyword 3" checkboxes and updated the user. The second updated deleted the previous records in my USER_KEYWORDS table.

Following is my console output showing the DELETE statement being executed:

  User Load (55.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
  Keyword Load (0.2ms)  SELECT "keywords".* FROM "keywords" WHERE "keywords"."id" IN (1, 3)
  Keyword Load (0.2ms)  SELECT "keywords".* FROM "keywords" INNER JOIN "user_keywords" ON "keywords".id = "user_keywords".keyword_id WHERE (("user_keywords".user_id = 5))
  AREL (0.2ms)  DELETE FROM "user_keywords" WHERE "user_keywords"."user_id" = 5 AND "user_keywords"."keyword_id" = 2
  AREL (0.1ms)  INSERT INTO "user_keywords" ("keyword_id", "user_id", "status", "created_at", "updated_at") VALUES (3, 5, 'f', '2012-06-12 13:15:43.912236', '2012-06-12 13:15:43.912236')

The :dependent option on has_many method says (http://guides.rubyonrails.org/association_basics.html#has_many-association-reference):

4.3.2.6 :dependent

If you set the :dependent option to :destroy, then deleting this object will call the destroy method on the associated objects to delete those objects. If you set the :dependent option to :delete_all, then deleting this object will delete the associated objects without calling their destroy method. If you set the :dependent option to :nullify, then deleting this object will set the foreign key in the associated objects to NULL.

This option is ignored when you use the :through option on the association. ##

I am unable to understand this behavior.Can anybody please elaborate or clarify in this?

解决方案

The delete statement only deleted the the association for keyword 2. When you submitted the form with two checkboxes checked, the system assumes that you want those two items associated, but none of the others that were not checked. Therefore, when it loaded the current user_keyword associations, it saw that there was one existing record that you did not choose to keep, so it deleted it. It then saw that there was one new one, so it created a new one.

If you want to only add relationships, and not remove relationships, then I would not recommend checkboxes, as checking a checkbox implies adding, but removing a check implies destroying the relationship (which is what rails did in this case). A button with AJAX functionality might make more sense. If you do want to keep checkboxes, but not allow rails to destroy relationships that weren't selected, then you would have to build the relationships yourself based on the parameters, and not rely on the rails magic to do the job for you.

Note that this is not related to the :dependent option, this is simply due to the relationship you have set up in the models and in the form.

这篇关于活动记录update_attribute执行对象(父)的关联对象(子)在DELETE查询时父母被更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆