My full code can be seen at https://github.com/andyw8/simpleform_examples
I have a join model ProductCategory
with the following validations:
This is a "racing condition" in the callback chain.
When you create a product it doesn't have any id before it is saved, therefore there is no product
in the scope of ProductCategory
.
Product.new(name: "modern times", category_ids:[1, 2]) #=> #
At that stage of validation (before saving), ProductCatgory
cannot assign any id to it's foreign key product_id
.
That's the reason you have association validations : so that the validation happens in the scope of the whole transaction
UPDATE: As said in the comment you still can't ensure presence of a product/category. There's many ways around depending on why you want do this (e.g direct access to ProductCategory through some form)
validates :product, presence: true, if: :direct_access?
validates :product, presence: true, on: "update"
... But indeed these are all compromises or workarounds from the simple @product.create(params)