Interesting.  Looks like has_and_belongs_to_many is also true by default,
and has_one and belongs_to are *false* by default.  This all strikes me
as at least a little bit arbitrary and possibly misleading.  Why "has_many"
but not "has_one"? And why is there a "validates_associated" when clearly
you have to just call the :validate option to do the inverse?

Any philosophical thoughts on why this might be this way?  Or even practical
ones :-)

        -glenn

Neal Clark wrote:
> that's interesting. i would have expected what you did too, but that's  
> not the default behavior of has_many.
> 
> http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
> 
> has_many(association_id, options = {}, &extension)
>    ...
>      Supported options        
>      ...
>        :validate
>          If false, don‘t validate the associated objects when saving  
> the parent object. true by default.
> 
> so looks like has_many tasks :default => false will get what you're  
> looking for.
> 
> 
> On Jul 13, 2009, at 6:15 PM, Glenn Little wrote:
> 
>> I've got something like this:
>>
>>  Project has_many   tasks
>>  Task    belongs_to project
>>
>> I have simple validation in each.  The problem is, if I do something
>> like:
>>
>>  p = Project.new
>>  p.tasks.build  # <-- not valid because of blank field
>>  p.tasks.build
>>  p.tasks.build
>>
>>  p.valid? -> false
>>
>> All good so far.  But if I look at the errors on p:
>>
>>  y p.errors
>>
>> I see something like:
>>
>>  errors:
>>    name:
>>    - can't be blank   # this is fine
>>    tasks:
>>    - is invalid
>>    - is invalid
>>    - is invalid       # these, not so much
>>
>> I do not have a call to "validates_associated" in my Project model, so
>> why is it picking up errors on the associated objects?
>>
>> Thanks in advance...
>>
>>              -glenn
>>
> 
> 
> > 

--~--~---------~--~----~------------~-------~--~----~
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
-~----------~----~----~----~------~----~------~--~---

Reply via email to