Craig Walker
Craig Walker

Reputation: 51707

Validations for a has_many/belongs_to relationship

I have a Recipe model which has_many Ingredients (which in turn belongs_to Recipe). I want Ingredient to be existent dependent on Recipe; an Ingredient should never exist without a Recipe.

I'm trying to enforce the presence of a valid Recipe ID in the Ingredient. I've been doing this with a validates :recipe, :presence => true (Rails 3) statement in Ingredient. This works fine if I save the Recipe before adding an Ingredient to it's ingredients collection. However, if I don't have explicit control over the saving (such as when I'm creating a Recipe and its Ingredients from a nested form) then I get an error:

Ingredients recipe can't be blank

I can get around this simply by dropping the presence validation on Ingredient.recipe. However, I don't particularly like this, as it means I'm working without a safety net.

What is the best way to enforce existence-dependence in Rails?

Things I'm considering (please comment on the wisdom of each):

Upvotes: 1

Views: 1141

Answers (1)

tsdbrown
tsdbrown

Reputation: 5058

Take a look at this:

https://rails.lighthouseapp.com/projects/8994/tickets/2815-nested-models-build-should-directly-assign-the-parent

In the past I think I've added a :on => :update option to the validator, helps tighten the net a little. I think the foreign key restriction in the DB is a good idea, although I rarely use it : (

Upvotes: 2

Related Questions