Andrew
Andrew

Reputation: 43153

Rails: Validate associated and saved?

I'm aware of validates_associated, but I'm not sure it does what I need it to do...

I have a model Photo which has_one Attachment -- I need to make sure the photo has an associated, valid, saved attachment before the photo itself saves.

Does validates_associated cover all of that, or is there a different/better way to do this?

Thanks!

Upvotes: 0

Views: 404

Answers (1)

Andrew Marshall
Andrew Marshall

Reputation: 97004

You're looking for validates_presence_of in addition to validates_associated. As noted in the API:

[validates_associated] will not fail if the association hasn’t been assigned. If you want to ensure that the association is both present and guaranteed to be valid, you also need to use validates_presence_of.

Upvotes: 1

Related Questions