Given that the “Rails Way” seems to be not to use foreign key constraints, I’m looking for an alternative that will allow me to validate that the row a foreign key references does in fact exist in TableA before I save an object in TableB with table_a_id.
The only resource I’ve found to do this so far (can’t find a link to the blog post it was mentioned in, was dated 2007) doesn’t appear to be compatible with Rails 3.2, so can anyone suggest a way of doing this?
I’m currently looking at creating a validator to manually assign to the relevant attributes in my models, but I can’t work out how to do it with validate_each(object, attribute, value).
There is a plugin that helps you with this for belongs_to associations: Validates existence of. But, maybe you can add your own validation? What about something like this: