私は、関連があるモデルではかなり複雑なビジネスロジックを持っています。私は、ユーザーが未完成の(無効な)モデルを将来の完成のために保存できるようにしたい。私のコントローラでは、私はinstance.save(validate: false)
と呼んでいます。保存後にRailsコールバックを復元する(validate:false)
私のアプリケーションから抽出した3つのモデルは次のとおりです。 関連する部分のみを抽出したため、モデルが過度に見られることがあります。
1)Container
モデル:
class Container < ApplicationRecord
Languages = %w(fr en de it es)
belongs_to :name, class_name: "Name", foreign_key: :name_id
accepts_nested_attributes_for :name
validates :name, presence: true
# create an empty model
def self.create_new
c = Container.new
c.name = Name.new
c.name.save
Languages.each do |l|
c.name.translations << NameItem.new(language: l, text: "")
end # Languages.each do |l|
c.description = Description.new
c.description.save
Languages.each do |l|
c.description.translations << DescriptionItem.new(language: l, text: "")
end # Languages.each do |l|
c
end
end
2)Name
モデル:
class Name < ApplicationRecord
has_many :translations, class_name: "NameItem", foreign_key: :parent_id
accepts_nested_attributes_for :translations
end
3)NameItem
モデル:命令の次のシーケンスを考える
class NameItem < ApplicationRecord
validates :language, presence: true
validate :text_validation
private
def text_validation
return if language.nil?
errors.add(:text, :blank_text, language: language) if text.nil? || text.size == 0
end
end
、私がでていますなぜ最後の命令(instance.valid?
) trueを返します。検証コールバックは無効になっているようですが、これが適切な診断であるかどうかはわかりません。 ?これはaccepts_nested_attributes_for
の使用に関連することができます
# create an invalid model (since empty)
instance = Container.create_new
# check that this instance is invalid
instance.valid? # returns false, this is the expected behavior
# save the model, skipping the validations
instance.save(validate: false)
# now instance.valid? will always return true, because it will
# skip the validations. How can I restore the callbacks ?
instance.valid? # returns true, but this is not the desired behavior,
# hence my question
私は次のような指示にコールバックを無効にし、再度有効にしようとしたが、無駄に。
[:create, :save, :update].each do |action|
Container.skip_callback(action)
end
[:create, :save, :update].each do |action|
Container.set_callback(action)
end
だから私の質問は:instance.save(validate: false)
を呼び出した後、命令は私がinstance.valid?
が実際に再検証チェックを実行し、falseを返すないように実行する必要が何をすべきか?
ユーザーがいくつかの属性を入力せずにレコードを作成できる場合は、作成イベントではなく、その存在をまったく検証しないでください。しかし、レコードのライフサイクルの後半で、ユーザーがこのレコードを公開したい(またはそれを使用して「完了」としてチェックする)場合は、検証を行う必要があります。 – MrYoshiji
ありがとうございます@MrYoshiji。この目的を達成するためのコードスニペットを提案してください。 –
あなたのアプリケーションでは、Container **が必要とする瞬間(イベント)で**属性の有無をチェックする必要がありますか?特定のコンテキストでコンテナを使用したい場合、使用可能なコンテナにattrsが存在する必要があります。 – MrYoshiji