0
我有一本書模型,一個標記模型和一個名爲bookstags的連接模型,它們看起來像下面的樣子。delete_all在與rails的連接中查找範圍
當我想讀的所有via_tags,Book.first.via_tags它工作的意願,
但是當我嘗試刪除所有相關via_tags,我試過Book.first.via_tags.delete_all() 而且我得到了一個異常。
ActiveRecord::StatementInvalid: SQLite3::SQLException: near "INNER": syntax error: DELETE FROM "tags" INNER JOIN "books_tags" ON "tags".id = "books_tags".tag_id WHERE (("books_tags".book_id = 25)) AND ("tags"."kind" = 0)
所以我想問的是,是否有可能使用聯接執行delete_all?
class Book < ActiveRecord::Base
has_many :tags, :through => :books_tags
end
class BooksTags < ActiveRecord::Base
belongs_to :book
belongs_to :tag
def self.with_via_tag
find(:all, :joins => :tag, :conditions => {:tags => {:kind => Tag.kind_index(:via)}})
end
end
class Tags
has_many :books_tags
has_many :books, :through => :books_tags
@kinds = [:via,:cate,:attr]
def self.kind_index kind
@kinds.index(kind)
end
end
你使用'delete_all'而不是'destroy_all'的任何原因? – 2011-04-09 10:00:21
我現在正在使用它,真的沒有理由,應該關閉這個問題嗎? – albb 2011-04-12 09:17:06