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.
Dfb5975ecda818fc5a0848a73e4736ae?d=identicon&s=25 Md. Kazim Zaidi (Guest)
on 2008-10-19 19: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.
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2008-10-20 23:03
(Received via mailing list)
Md. Kazim Zaidi 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
Dfb5975ecda818fc5a0848a73e4736ae?d=identicon&s=25 Kazim Zaidi (Guest)
on 2008-10-21 01:09
(Received via mailing list)
On Tue, Oct 21, 2008 at 2:33 AM, Mark Reginald James
<mrj@bigpond.net.au>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 Zaidi
Blog: http://tuxplayground.blogspot.com
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2008-10-21 23:49
(Received via mailing list)
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.

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