My rails 4 app has 3 models:
- Store (has_many :products)
- Product (has_many :product_fields and belongs_to :store and accepts_nested_attributes_for product_fields
- Product_fields (has a belongs_to :product)
Product only has store_id and id fields. Product_fields has string_content and text_content. Basically, right now my store model looks like:
class Store < ActiveRecord::Base
has_many :products
accepts_nested_attributes_for :products, :allow_destroy => true, :reject_if => :product_empty
private
def product_empty(a)
a[:product_fields_attributes][:text_content].blank?
end
end
If I create a new store without filling in text_content, the models correctly reject it (and no product or product_fields are created). Unfortunately, the problem is that if I actually do fill in text_content, then it still doesn't create it.
My rails console looks like:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"nFUg4ynXYyg99rPPPoa3uO/iHP4LT1XlOz3Vm3Zm4Z0=", "store"=>{"name"=>"Test", "products_attributes"=>{"0"=>{"type_of"=>"Book", "product_fields_attributes"=>{"0"=>{"text_content"=>"test1"}}}}}, "commit"=>"Create Store"}
My question is: How do I get the reject_if method to work on nested models? So, to be clear, I don't want to validate the nested models, I just want to not save products if it's associated product_field text_content is blank?.