要截斷一個ActiveRecord表,我可以做如何在軌中截斷連接表?
Category.destroy_all
或
Post.destroy_all
一個人如何去截斷categories_post
表?
要截斷一個ActiveRecord表,我可以做如何在軌中截斷連接表?
Category.destroy_all
或
Post.destroy_all
一個人如何去截斷categories_post
表?
對於真實的TRUNCATE
,可以使用execute
來運行原始SQL。
ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{table_name}")
您使用模型的示例沒有執行真正的TRUNCATE
查詢。
destroy_all
不是TRUNCATE
的表格。它「通過實例化每條記錄並調用其銷燬方法來破壞匹配條件的記錄」(link)。delete_all
更接近 - 它忽略回調 - 但仍然不是TRUNCATE
。使用execute
方法刪除數據庫中的行而不創建任何模型實例。
而且,實際TRUNCATE
查詢,至少在MySQL,將重新設置主鍵自動遞增,讓你插入的下一個記錄將有1
我想你的連接表叫做categories_posts。 CategoryPost.destroy_all應該工作,如果沒有,也許你需要在模型中指定表名(CategoryPost)
set_table_name "categories_posts"
更新,還沒有一個CategoryPost模型,所以應該創建:
class CategoryPost < ActiveRecord::Base
set_table_name "categories_posts"
end
ID連接的表沒有一個關聯模型。例如'CategoryPost' – 2010-09-09 09:59:30
您可以創建它 – jordinl 2010-09-09 10:11:09