在rails中,如何在不删除真实记录的情况下销毁"连接表项"?

Cro*_*lio 11 join ruby-on-rails has-many-through destroy

我现在感到困惑,我不知道如何删除/销毁连接表中的记录:


class Task < ActiveRecord::Base
  belongs_to :schema
  belongs_to :to_do
end

class Todo < ActiveRecord::Base
  belongs_to :schema
  has_many :tasks
end

class Shcema < AcitveRecord::Base
  has_many :todos
  has_many :tasks, :through => :todos
end
Run Code Online (Sandbox Code Playgroud)
>> sc = Schema.new
>> sc.tasks << Task.new
>> sc.tasks << Task.new
>> sc.tasks << Task.new
...
>> sc.tasks.delete(Task.first) # I just want to delete/destroy the join item here.
# But that deleted/destroyed the Task.first.
Run Code Online (Sandbox Code Playgroud)

如果我只想破坏关系项,我该怎么办?

小智 7

如果要删除sc中的所有任务:

sc.tasks.delete_all
Run Code Online (Sandbox Code Playgroud)

如果要删除sc中的特定任务:

sc.tasks.delete_at(x) where x is the index of the task

or

sc.tasks.delete(Task.find(x)) where x is the id of Task
Run Code Online (Sandbox Code Playgroud)

我希望有所帮助.