SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Question about model associations...

    If I specify a model that has_many of another model and I do the other model as belongs_to when I delete the model with has_many will it delete all of the associated belongs_to if I use the destroy command? Thanks
    Have a good day.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,260
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    AFAIK, "destroy" only deletes the files created by "generate" (doesn't undo the database).

    I have a tutorial app "tasty" http://www.tutorialspoint.com/ruby-o...-2.1/index.htm so I made a copy of it to experiment with.
    It has a User model with has_many :entries in it. I did
    Code:
    C:\ruby\tasty>ruby script/destroy model User
        notempty  db/migrate
        notempty  db
              rm  db/migrate/20080904145346_create_users.rb
              rm  test/fixtures/users.yml
              rm  test/unit/user_test.rb
              rm  app/models/user.rb
        notempty  test/fixtures
        notempty  test
        notempty  test/unit
        notempty  test
        notempty  app/models
        notempty  app
    *removed 4 files.
    It did not delete the "child" models Entry (or Tag or Tagging beneath Entry) and entry.rb still has the belongs_to in it
    Code Ruby:
    class Entry < ActiveRecord::Base
     
      validates_uniqueness_of :url
     
      # -- Tell Entry that it has a unique User associated with it.
      belongs_to :user
     
      has_many :taggings
      has_many :tags, :through => :taggings

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2003
    Location
    Akron, OH, USA
    Posts
    106
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Darren884 View Post
    If I specify a model that has_many of another model and I do the other model as belongs_to when I delete the model with has_many will it delete all of the associated belongs_to if I use the destroy command? Thanks
    You can use either of these two destroy child rows:

    orders.delete_all
    Invokes the associationís delete method on all the child rows.

    orders.destroy_all
    Invokes the associationís destroy method on all the child rows.


    Just replace "orders" with the name of your association.

  4. #4
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys.
    Have a good day.


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
  •