12

混乱しています。結合テーブルのレコードを削除/破棄する方法がわかりません:


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

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

class Schema < ActiveRecord::Base
  has_many :todos
  has_many :tasks, :through => :todos
end

>> 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.

リレーション アイテムを破棄したいだけの場合はどうすればよいですか?

4

3 に答える 3

7

sc のすべてのタスクを削除する場合:

sc.tasks.delete_all

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

それが役立つことを願っています。

于 2011-08-03T20:25:19.207 に答える
2

オブジェクトを使用せずに結合テーブルからすべてのレコードを削除する場合は、amenities_lodgings 次を使用できます。

ActiveRecord::Base.connection.execute("DELETE  from amenities_lodgings")
于 2016-02-15T10:02:42.257 に答える