簡體   English   中英

rails accepts_nested_attributes和:reject_if

[英]rails accepts_nested_attributes and :reject_if

我有一個具有嵌套屬性的表單。 現在在我的: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

這應該適合你。 基本上這意味着在自定義功能中,您可以執行任何您想要的操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM