2012-02-17 51 views
3

它看起來像刪除&銷燬同時處理has_many時從db刪除記錄。無論如何不要這樣做。換句話說,我想在將它傳遞給方法之前修剪has_mnay集合,但我不希望我的更改持久保存到數據庫。在試圖在控制檯上,它似乎立即刪除,當我做從has_many中刪除記錄而不從db中刪除? (導軌2.3.5)

second_acct = users.accounts[1] 
users.accounts.delete(second_acct) 

我的使用情況會是這樣的我想只有通過支票賬戶下降到一個方法,所以我想請從這些帳戶用戶。

+0

爲了確保我在這裏清楚,我不想觸摸數據庫,我只想刪除內存中的關係 – Joelio 2012-02-17 15:50:01

+1

你有沒有找到一種方法來做到這一點? – 2013-05-16 15:33:08

回答

0
second_acct = users.accounts[1] 
second_acct.update_attribute(:user_id, nil) 

這應該工作。

或者這樣:

second_acct.user = nil 
second_acct.save 
-2

如何在您的協會成立?

collection.delete(object, …) 
    Removes one or more objects from the collection by setting their foreign keys to NULL. Objects will be in addition destroyed if they’re associated with :dependent => :destroy, and deleted if they’re associated with :dependent => :delete_all. 

所以,如果你不想讓你的記錄被刪除,僅該協會被清除,刪除或:dependent => :destroy選項:dependent => :delete_all

+0

我有依賴=>:銷燬,當我真的刪除它們時,我希望它們從數據庫中清除。在上面的情況下,我不想觸摸數據庫,只是從內存中的集合中刪除它們。 – Joelio 2012-02-17 15:49:38