Forum: Ruby on Rails The cookbook - category delete?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Marko (Guest)
on 2007-05-23 16:12
How can I delete category and set all recipes linked to that category to
"no category"?

The tutorial  at O'Reilly says nothing about that...
Conrad T. (Guest)
on 2007-05-23 16:23
(Received via mailing list)
Hi, you'll simply have to change
Marko (Guest)
on 2007-05-23 16:40
Conrad T. wrote:
> Hi, you'll simply have to change

:-) So do I have to make a new Category with name 'no category' and than
link all recipes to new 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?
mixplate (Guest)
on 2007-05-23 17:59
what i did was make a category titled category and make it so its not
editable or deletable by the user.

that way, there will be at least one category for the user to keep notes
attached to "categories has many note, notes belong to category"
relationship.


Marko wrote:
> How can I delete category and set all recipes linked to that category to
> "no category"?
>
> The tutorial  at O'Reilly says nothing about that...
Bill W. (Guest)
on 2007-05-23 18:00
(Received via mailing list)
Hi Marko,

Marko wrote:

> Conrad T. wrote:
>> Hi, you'll simply have to change
>
> :-) 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...

> 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
Chris H. (Guest)
on 2007-05-23 18:13
(Received via mailing list)
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.
This topic is locked and can not be replied to.