Forum: Ruby on Rails Trouble validating associated objects

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Md. Kazim Z. (Guest)
on 2008-10-19 21:28
(Received via mailing list)
Hello everyone,
This is my first post to this list, so kindly remind me if I do
something wrong. :-P

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.
Mark Reginald J. (Guest)
on 2008-10-21 01:03
(Received via mailing list)
Md. Kazim Z. wrote:
>
> *** app/models/book.rb ***
> 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)


--
Rails Wheels - Find Plugins, List & Sell Plugins -
http://railswheels.com
Kazim Z. (Guest)
on 2008-10-21 03:09
(Received via mailing list)
On Tue, Oct 21, 2008 at 2:33 AM, Mark Reginald J.
<removed_email_address@domain.invalid>wrote:

> > will just fail because the associated object is nil. I'm trying to
> > end
> > u = User.new(:name => 'Shakespeare', :age => 444)
>
> 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. :-)

--
- Kazim Z.
Blog: http://tuxplayground.blogspot.com
Mark Reginald J. (Guest)
on 2008-10-22 01:49
(Received via mailing list)
Kazim Z. wrote:
>
> On Tue, Oct 21, 2008 at 2:33 AM, Mark Reginald J. <removed_email_address@domain.invalid
>     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.

--
Rails Wheels - Find Plugins, List & Sell Plugins -
http://railswheels.com
This topic is locked and can not be replied to.