SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Model has_many; delete all children?

    Hi,

    If I have a model that has_many of another table, how can I make it so that when i call that model's destroy method that it also destroys all the children? Is there a method to do this?

    Currently I do it like this:
    Code:
     @shop.destroy
     Item.destroy_all 'shop_id = ' + @params[:id]
    It works but I was wondering if maybe there was a better way

    Thanks again,
    Mitch

  2. #2
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    Yakima WA.
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    has_many :foos, :dependant => true

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ezmobius1
    has_many :foos, :dependant => true
    Thank you, that is what I was looking for. And I also found the documentation on it in the APIs:
    :dependent - if set to :destroy (or true) all the associated objects are destroyed alongside this object. Also accepts :nullify which will set the associated objectís foreign key field to NULL. May not be set if :exclusively_dependent is also set.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •