私のデータベーステーブル内で複数のレコードを破壊しようとしています。リストの列は同じ名前ですが、リンクを破棄するとエラーが表示されます: 'bookmarks_posts'エラーが私のコントローラの行にあると言います。HABTM参照テーブル内の複数のオブジェクトを削除する
if @bookmarks.destroy_all
なぜ結合テーブルが必要ですか?どうすれば変更できますか?また、私は指定されたブックマークテーブルの外に何かを破棄したくない。
def destroy
@list = Bookmark.find(params[:id])
@bookmarks = Bookmark.where(:list => @list.list)
if @bookmarks.destroy_all
redirect_to bookmarks_url
end
end
def show
@lists = Bookmark.where(user_id: current_user.id).where(post_id: nil)
@list = Bookmark.find(params[:id])
@bookmarks = Bookmark.where.not(post_id: nil).where(list: @list.list)
@posts = Post.where(:id => @bookmarks.map(&:post_id))
end
私のショービューで、私が使用してこの:
- 破壊し、ショーclass CreateBookmarks < ActiveRecord::Migration
def change
create_table :bookmarks do |t|
t.string :list
t.references :user, index: true, foreign_key: true
t.references :post, index: true, foreign_key: true
t.timestamps null: false
end
end
end
マイコントローラ:
私のテーブルの移行(それが何かを変更した場合、私は、sqlite3のを使用しています)
<%= link_to 'Destroy', @list, method: :delete %>
マイモデル:
class Bookmark < ActiveRecord::Base
has_and_belongs_to_many :users
has_and_belongs_to_many :posts
end
質問をモデルコードで更新してください。 – Pavan
ちょうど私がタイトルで述べたものよりも何もありませんでしたが、両方のテーブルの間のHABTMの関係、または私はそう思っています... – kvadrats