
例:
Model.where(parameters).first_or_create do |m| # execute this regardless of whether record exists or notend
类似地,方法find_or_create_by在任一场景中都不执行块.
我可以使用if-else语句,但这会复制我的代码……
解决方法 根据 API,示例中的块仅在记录不存在时才会执行.但是你不需要块,也不需要if-else:m = Model.where(parameters).first_or_createm.some_method # executes regardless of whether record exists or not
请注意,对象上的某些验证可能已失败,因此m可能尚未持久保存到数据库.
总结以上是内存溢出为你收集整理的ruby-on-rails-3.2 – 在Rails中,对两个条件执行block:first_or_create全部内容,希望文章能够帮你解决ruby-on-rails-3.2 – 在Rails中,对两个条件执行block:first_or_create所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)