ryanb/nested_form — Issue #107: embedded attribute callback not working
Author: chabgood
Created: 2011-10-24 02:32:59+00:00
State: open

I have an association, problem has_many solutions, solution has_one product.

The callback in the product model is being called but when I try and set the item it does not work in a before_save:

form:

<%= f.fields_for :solutions %>

partial:

<%= f.fields_for :product_attributes do |f| %>
<%= f.text_field :name %>
<% end %>
<%= f.label :how_many %>
<%= f.text_field :how_many %>

<%= f.label :how_much %>
<%= f.text_field :how_much %>

<%= f.label :how_often, 'How Often, per/' %>
<%= f.select :how_often, Product::How_often %>

<%= f.label :details %>
<%= f.text_area :details, :size => '20X5' %>

<%= f.link_to_remove "Remove solution" %>

Product model:

before_save do |record|
p = Product.find_by_name(record.name)
unless p.blank?
record.name = p.name
end
return true
end

It keep creating a new object even though the one in the form does exist in the db. Ideas?