2009-11-16 32 views
3
create_table "tags_pages", :id => false do |t| 

    t.integer "tag_id", "page_id" 

end 

add_index "tags_pages", "tag_id" 
add_index "tags_pages", "page_id" 

activerecord如何在此表上工作?我想插入和刪除新行。對不起,如果這是一個noob問題。如何使用ActiveRecord從連接表中刪除行?

回答

8

假設您有一個頁面和一個標籤。

# This will add a "tags_pages" entry, linking one page to one tag 
page.tags << tag 

# This will delete the appropriate "tags_pages" entry 
page.tags.delete(tag) 

您也可以刪除鏈接到一個網頁與clear方法的所有標籤。

page.tags.clear