Regarding validates_associated…
Let’s say I have:
article belongs_to author
But for whatever reason, I want an article to also be written
anonymously and therefore not require an author. Then I have:
Article:
belongs_to :author
validates_associated :author
But I DON’T have validates_presence_of. What I want to do is validate
that an author is valid --if it is specified–.
- I want to allow for the case where article.author_id = nil.
- I DO NOT want to allow for the case where article.author_id =
SOME_INVALID_ID
During testing, I tried this:
article = Article.new
article.author_id = nil
assert c.valid? (True – working so far)
article.author_id = 5
assert c.valid? (True – because author 5 is valid)
article.author_id = 999
assert c.valid? (True!?! – shouldn’t be!)
So, how can one protect for the case where the associated link doesn’t
refer to an existing database row at all?
Can someone give me an example of a successful use of
validates_associated that is NOT coupled to validates_presence_of?
Afterall, it seems quite pointless to have validates_associated if it
returns true for an object that isn’t even in the database.
Jake