The cookbook - category delete?

Hi, you'll simply have to change

i believe what you want to do is:

Recipe < AR::Base
  belongs_to :category
  belongs_to :author
end

Category < AR::Base
  has_many :recipes, :dependent => :nullify
end

Author < AR::Base
  has_many :recipes, :dependent => :nullify
end

what this does is if you delete/destroy a category or author, the
recipe's foreign keys will be set to null, which in effect says that
the recipe has no category or author, depending on which you
deleted/destroyed.

Hi Marko,

Marko wrote:

Conrad Taylor wrote:

Hi, you'll simply have to change

:slight_smile: So do I have to make a new Category with name
'no category' and than link all recipes to new category?

Yes. Because the belongs_to relationship requires that a recipe must have a category.

What if I have Author of recipe...

table Authors with fields:

id
first_name
last_name
city
state
street
zip
phone

Now how do I set "no author" here?

The answer depends on the relationship you want to maintain between the various entities. Do you want to make sure that you every Recipe has an Author? What about having Authors hanging around with no contributions? The table definition above has no foreign keys and so, as it stands, you currently have no way to link it to Recipes. These are basic database design questions/concepts that really have nothing to do with Rails. You might want to Google 'database design' and spend a little time getting your arms around the basics.

Best regards,
Bill