Problem with validates_presence_of for the parent object

0 views
Skip to first unread message

Sean Schofield

unread,
Jun 17, 2008, 9:48:43 AM6/17/08
to attribute_fu
I'm probably missing something obvious here. I have two models:

Option
has_many :costs, :as => :costable, :attributes => true

Cost
belongs_to :costable, :polymorphic => true
validates_presence_of :costable

When I am creating a new form, I am not able to create costs at the
same time. The validation keeps complaining that the cost is
invalid. I quickly figured out the problem was that costable had not
been set yet. Everything works fine if I add the cost after saving
the new option first.

Any suggestions for how to address this elegantly with a_f?

TIA,

Sean

James Golick

unread,
Jun 17, 2008, 10:15:06 AM6/17/08
to attrib...@googlegroups.com
usually you have to validate presence of costable_id. However, I would put the validation in the parent, because I'm not sure that you can validate a belongs to when you're creating both @ once.
Reply all
Reply to author
Forward
0 new messages