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
| SitePoint Sponsor |





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.


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
*removed 4 files.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
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
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.





Thanks guys.
Have a good day.
Bookmarks