Simple example.

class Car < ActiveRecord::Base
  has_one :engine

  validates_presence_of :engine
  validates_associated :engine
end

class Engine < ActiveRecord::Base
  belongs_to :car

  validates_presence_of :car
end

@car = Car.new
@car.valid? # => false
@car.errors # => no engine obviously

@car.build_engine
@car.valid? # => false

How come??

@car.errors # => engine has no car!
@car.engine.car # => nil

Since we validate_associated :engine, engine runs its own validations
and finds that  validates_presence_of car is false, since car is nil.
Shouldn't engine know the car that built it?

Just for fun I do this:

@car.engine.car = @car
@car.valid? # => true

Shouldn't this work by default?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to