モデルの条件付き検証の作成に関する作業。これをチェックするために私はparent.parent_attribute @現在Parent.parent_attribute ==「真」子モデルレールの親属性へのアクセス
validates :departure_date, presence: true, future: true, :if => :awesome_method?
belongs_to :parent
def awesome_method?
if @parent.parent_attribute == "true"
true
else
false
end
end
はペイロードがそれを持っているにもかかわらず、nilを返している場合。チェックする必要があります私はそれがまだ保存されていないので、parent_attributeにアクセスできない問題に遭遇していると思います...検証を設定する前に親に値があることを確認するにはどうしたらいいですか?
更新 わかりやすくするために、私は親と子を同時に作成しています。 私はParent.find...
を使用しようとすると、私は= CORRECT_INVOICE_NAME
「PARENT_ID」と親が見つかりませんでしたバック
を入手アップデート2 親と子の両方に作成がありますが、次のJSONは子を持つ親を作成するものです。
{ “parent": { “parent_id": "PLZDONTSAVE", “data": 2525.25, “parent_attribute": true, “child_attributes":[ { “child_toy": “123", “child_data": “ABC" } ] } }
親コントローラ
def create @parent = Parent.new(parent_params) if @parent.save render json: @parent, status: :created, location: @parent else render json: @parent.errors, status: :unprocessable_entity end end def parent_params params.require(:parent).permit(:parent_id, :parent_attribute, child_attributes:[:child_toy, :child_data, ]) end
'parent.parent_attribute'が返されます > NoMethodError(nil:NilClassのための未定義メソッド' parent_attribute ') – CheeseFry
あなたは親と子を同時に作成するコードを表示できますか? – tbreier
私はオブジェクトを同時に構築することによって私が意味するものを実証するために私の質問を更新しました – CheeseFry