:reject_if
働かないことについての質問はかなりあります。これがもう1つのケースです。は:reject_if
完全にバイパスされました。
ユーザーモデルのコードは次のとおりです。
class User < ActiveRecord::Base
attr_accessible :name, :login, :email, :password, :user_type, :user_levels_attributes, :as => :role_new
has_many :user_levels, :dependent => :destroy
accepts_nested_attributes_for :user_levels, :reject_if => proc { |a| a['position'].blank? }, :allow_destroy => true
end
ユーザーには、ユーザーの位置情報を保持する多くのユーザー レベルがあります。user_level のモデルは次のとおりです:</p>
class UserLevel < ActiveRecord::Base
belongs_to :user
validates :position, :presence => true
end
ポジションのないユーザーは保存されるべきではなく、それがすべきこと:reject_if
です。しかし、試してみましたproc, lambda, 'position' and :position
。最後に:reject_if => true
、 の実行を強制しようとさえしました:reject_if
。空白の位置を持つユーザーが保存されましたが、上記のいずれも で空白の位置をキャッチしませんでしたuser_level
。:reject_if
完全にバイパスされたようです。
上記のコードの何が問題なのですか? ありがとうございました。