gpt4 book ai didi

ruby-on-rails - rails 接受_nested_attributes 和 :reject_if

转载 作者:行者123 更新时间:2023-12-03 16:14:59 31 4
gpt4 key购买 nike

我有一个带有嵌套属性的表单。现在在我的 :reject_if =>声明我想检查嵌套模型上的属性,比如 first_record?有没有办法访问这样的方法?在我看来,您只能访问提交的属性哈希,例如检查字段是否为空。谢谢!

最佳答案

根据文档 http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

或者, :reject_if 也接受使用方法的符号:

class Member < ActiveRecord::Base
has_many :posts
accepts_nested_attributes_for :posts, :reject_if => :new_record?
end

class Member < ActiveRecord::Base
has_many :posts
accepts_nested_attributes_for :posts, :reject_if => :reject_posts

def reject_posts(attributed)
attributed['title'].blank?
end
end

这应该对你有用。基本上这意味着在自定义函数中你可以做任何你想做的事情。

关于ruby-on-rails - rails 接受_nested_attributes 和 :reject_if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13683948/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com