如何更新灯具的列以便仅使用update_column命令临时使用。 现在,我有以下运行罚款命令:如何更新Rails中的灯具列Minitest
name = names(:one)
role = roles(:one)
name.role_id = role.id
assert name.save
它运行良好,但没有任何有效的方式做到这一条线像name.update_column( - , - - )?
如何更新灯具的列以便仅使用update_column命令临时使用。 现在,我有以下运行罚款命令:如何更新Rails中的灯具列Minitest
name = names(:one)
role = roles(:one)
name.role_id = role.id
assert name.save
它运行良好,但没有任何有效的方式做到这一条线像name.update_column( - , - - )?
感谢@richfisher为您的答案,后来我找出了另一种方式来做到这一点。 update_attributes
是不是在测试中使用一个好主意,因为update_attributes方法问题是
It runs callbacks and validations
,通常我们不希望在测试案例运行这些东西相反我们可以使用update_attributes update_column这样的
name.update_column(:role_id, roles(:one).id)
的优势利用update_column的是
It did not run callbacks and validations
name = names(:one)
name.update_attributes(role_id: roles(:one).id)
请添加一些解释。您的答案目前被标记为“低质量”,最终可能会被移除。 –
**缺点**(有时)也是它不运行回调或更新。 可以使用update_column保存无效记录,并且有时会导致伪造测试结果。 –