2010-08-30 10 views
9

मैं अब भ्रमित हो, मैं हटाने के बारे में/एक में शामिल होने तालिका में एक रिकॉर्ड को नष्ट नहीं जानता:रेल में, वास्तविक रिकॉर्ड को हटाने के साथ 'टेबल आइटम में शामिल हों' को कैसे नष्ट करें?


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 

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

क्या मैं अगर मैं कर सकते हैं बस संबंध आइटम को नष्ट करना चाहते हैं?

उत्तर

6

आप अनुसूचित जाति में सभी कार्य को हटाना चाहते हैं:

sc.tasks.delete_all 

आप अनुसूचित जाति में एक विशेष कार्य को हटाना चाहते हैं:

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 

मुझे आशा है कि मदद करता है।

1

आप किसी भी वस्तु का उपयोग किए बिना amenities_lodgings तरह तालिका में शामिल होने से सभी रिकॉर्ड को नष्ट करना चाहते हैं, तो आप उपयोग कर सकते हैं:

ActiveRecord::Base.connection.execute("DELETE from amenities_lodgings") 
संबंधित मुद्दे