在 Rails 中,如何销毁“连接表项”?而不删除真实记录?
我现在很困惑,我不知道如何删除/销毁连接表中的记录:
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.
如果我只想销毁关系项,我该怎么办?
I get confuse now, I don't know how to delete/destroy a record in a join table:
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.
What can I do if I just want to destroy the relation item?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想删除 sc 中的所有任务:
如果您想删除 sc 中的特定任务:
我希望有所帮助。
If you want to delete all tasks in sc:
If you want to delete a specific task in sc:
I hope that helps.
如果您想从连接表(如
amenities_lodgings
)中删除所有记录而不使用任何对象,您可以使用:If you want to delete all records from join table like
amenities_lodgings
without using any object you can use:删除所有加入记录
如果要删除所有加入记录,可以使用
.clear
:Delete All Join Records
If you want to delete all the join records, you can use
.clear
: