2010/5/7 Sergio Cambra .:: entreCables S.L. ::. <[email protected]>:
> On Viernes, 7 de Mayo de 2010 01:03:38 Iñaki Baz Castillo escribió:
>> Hi, I'm not sure if the following is a common issue in ActiveRecords
>> or in ActiveScaffold and how it manages models in subforms:
>>
>> - Client has_many Routes (through "client_id" in "routes" table as usual).
>>
>> - In Client model I need this validation:
>>     validates_presence_of :client_id, :if => Proc.new { |route|
>> route.enabled }
>
> Validations should be added to association:
> validates_presence_of :client, :if => Proc.new { |route| route.enabled }
>
>>
>> - I need such validation because it's also possible to directly create
>> Routes in a separate way (without the need of creating a Client and a
>> Route(s) belonging ot it). In such case I need to ensure that the
>> route entry belongs to a Client just if the "enabled" field is checked
>> (in the Clientcontroller subform for Route such checkbox is hidden).
>>
>> - If I want to create a Client with a Route (by filling the subform)
>> then I get this error:
>>     "client_id cannot be empty"
>>
>
> You get that error because client is not saved when validations are checked,
> so it has no id and client_id is still nil. If you set the validation in
> client association, it will work.
>
>> I think this error occurs because AS creates first the Route model
>> (just in memory, not yet in DB) for which, of course, there is not yet
>> a client_value, am I right? what is the workaround for this?
>
> There is a client in route, but there is no client_id.

Great to know!


>The actual code is
> like:
> @record = Client.new [some params]
> @record.routes.build [some params]
> @record.save if @record.valid? && @record.routes.all?(&:valid?)
>
> Associations are set when valid? is called, but primary keys aren't set
> because records are not saved yet.

This is exactly what I wanted to understand :)


Thanks a lot.


-- 
Iñaki Baz Castillo
<[email protected]>

-- 
You received this message because you are subscribed to the Google Groups 
"ActiveScaffold : Ruby on Rails plugin" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/activescaffold?hl=en.

Reply via email to