Trouble validating associated objects

Hello everyone, This is my first post to this list, so kindly remind me if I do something wrong. :stuck_out_tongue:

When parent objects are saved, all the new children are saved. However, when the child objects validate themselves, they fail because the foreign key is nil. Domain logic in my application requires complex validations, which will just fail because the associated object is nil. I'm trying to illustrate using an over-simplified example:

(also pasted at http://pastie.org/295133) *** Schema *** User(id, name, age) Book(id, title, author_id)

*** app/models/user.rb *** class User < ActiveRecord::Base   has_many :books, :foreign_key => 'author_id' end

*** app/models/book.rb *** class Book < ActiveRecord::Base   belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'

  validates_presence_of :author_id end

*** ./script/console *** u = User.new(:name => 'Shakespeare', :age => 444) u.books.build(:title => 'The Comedy of Errors') u.save!   ActiveRecord::RecordInvalid: Validation failed: Books is invalid

Bad Solution: Remove validates_presence_of :author_id from Book model. This makes the model unsafe, but things work.

I hope I'm not following the best practices and somebody enlightens me. But if this is meant to be in this way, then how do I implement my validations on a Book?

Any comment/suggestion/hint appreciated.

Md. Kazim Zaidi wrote:

Hello everyone, This is my first post to this list, so kindly remind me if I do something wrong. :stuck_out_tongue:

When parent objects are saved, all the new children are saved. However, when the child objects validate themselves, they fail because the foreign key is nil. Domain logic in my application requires complex validations, which will just fail because the associated object is nil. I'm trying to illustrate using an over-simplified example:

(also pasted at http://pastie.org/295133) *** Schema *** User(id, name, age) Book(id, title, author_id)

*** app/models/user.rb *** class User < ActiveRecord::Base   has_many :books, :foreign_key => 'author_id' end

*** app/models/book.rb *** class Book < ActiveRecord::Base   belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'

  validates_presence_of :author_id end

*** ./script/console *** u = User.new(:name => 'Shakespeare', :age => 444) u.books.build(:title => 'The Comedy of Errors') u.save!   ActiveRecord::RecordInvalid: Validation failed: Books is invalid

Bad Solution: Remove validates_presence_of :author_id from Book model. This makes the model unsafe, but things work.

I hope I'm not following the best practices and somebody enlightens me. But if this is meant to be in this way, then how do I implement my validations on a Book?

Use validates_presence_of :author instead.

Currently you also have to do the build like u.books.build(:title => 'The Comedy of Errors', :author => u)

Thanks a lot! That does work. Currently? Is it about to be changed in 2.2?

The API doesn’t document this explicitly, and the fact that I need to pass

author as parameter too isn’t very obvious, IMHO.

Thanks again. :slight_smile:

Kazim Zaidi wrote:

On Tue, Oct 21, 2008 at 2:33 AM, Mark Reginald James <mrj@bigpond.net.au Currently you also have to do the build like     u.books.build(:title => 'The Comedy of Errors', :author => u)

Thanks a lot! That does work. Currently? Is it about to be changed in 2.2?

The API doesn't document this explicitly, and the fact that I need to pass author as parameter too isn't very obvious, IMHO.

I can't see it in current trunk. Yes it does violate the POLS.