开发者

Delete something through relationship table

开发者 https://www.devze.com 2023-02-25 05:54 出处:网络
i have 4 models. I want to delete question, but now i cant. Dont why know. I think, firstly i need delete answer on this questions, then delete inquiry, and then questions itself. Right. But how i can

i have 4 models. I want to delete question, but now i cant. Dont why know. I think, firstly i need delete answer on this questions, then delete inquiry, and then questions itself. Right. But how i can do it?

there my models:

-respondents_model

class Respondent < ActiveRecord::Base
  has_many :inquiries
  has_开发者_如何学JAVAmany :questions, :through => :inquiries
  has_many :answers,   :through => :inquiries
end

-answer_model

class Answer < ActiveRecord::Base
  belongs_to :inquiry
  belongs_to :question

  validates_uniqueness_of :inquiry_id
end

-question_model

class Question < ActiveRecord::Base
  has_one :answer, :through => :inquiry , :dependent => :destroy
  belongs_to :inquiry , :dependent => :destroy
end

-inquiry_model

class Inquiry < ActiveRecord::Base
  belongs_to :question
  belongs_to :respondent
  has_one    :answer
end

and my question_controller

  def destroy
    @question.destroy
    head :ok
  end


You don't need to delete answers, because they will be automatically deleted as far as you set :dependent => :destroy. So you just need to call:

Also you need to specify WHAT EXACT question are you going to destroy: Question.find params[:id]

def destroy
  @question = Question.find params[:id]
  @question.destroy
  head :ok
end
0

精彩评论

暂无评论...
验证码 换一张
取 消