轨道3:我是否需要给回报的before_save回调如此的object.save工作? [英] rails 3 : Do i need to give return true in a before_save callback for an object.save to work?

查看:146
本文介绍了轨道3:我是否需要给回报的before_save回调如此的object.save工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 类用户
  before_save:set_searchable

  高清set_searchable
    self.searchable = true如果self.status ==:活跃
  结束
结束

>> U = User.last
>> u.save
假
 

u.save总是返回false。如果我删除before_save它的工作原理 另外,如果我给一回真正的before_save它的工作原理

所以我需要给return语句在before_save? 将ActiveRecord的保存对象,如果before_save返回false?

我在哪里可以查看回调和工作流程的完整文档。

在此先感谢

解决方案

从:<一href="http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html">http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

  
    

如果一个before_ *回调返回false,所有后来的回调和相关的动作被取消。如果after_ *回调返回false,所有后来的回调被取消。回调,通常运行在它们被定义的顺序,除定义为模型,该模型最后被调用的方法的回调。

  

所以,是的。

Class User  
  before_save :set_searchable

  def set_searchable  
    self.searchable = true if self.status == :active  
  end  
end  

>> u = User.last  
>> u.save  
false  

u.save always return false. If i remove the before_save it works also if i give a return true in before_save it works

so do i need to give return statements in before_save ? will ActiveRecord saves an object if the before_save returns false ?

Where can i see a full documentation regarding callbacks and its workflow .

Thanks in advance

解决方案

From: http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

If a before_* callback returns false, all the later callbacks and the associated action are cancelled. If an after_* callback returns false, all the later callbacks are cancelled. Callbacks are generally run in the order they are defined, with the exception of callbacks defined as methods on the model, which are called last.

So, yes.

这篇关于轨道3:我是否需要给回报的before_save回调如此的object.save工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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