Say I have a basic Rails app with a basic one-to-many relationship where each comment belongs to an article:
$ rails blog
$ cd blog
$ script/generate model a
I've also been investigating this topic and here is my summary:
The root cause why this doesn't work OOTB (at least when using validates_presence_of :article and not validates_presence_of :article_id) is the fact that rails doesn't use an identity map internally and therefore will not by itself know that article.comments[x].article == article
I have found three workarounds to make it work with a little effort:
class Article < ActiveRecord::Base
has_many :comments, :inverse_of => :article
endThis last solution was bot yet mentioned in this article but seems to be rails' quick fix solution for the lack of an identity map. It also looks the least intrusive one of the three to me.