RESTful破坏Rails中的多态关联? [英] RESTfully destroy polymorphic association in Rails?

查看:103
本文介绍了RESTful破坏Rails中的多态关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在保持此RESTful的同时,我如何破坏关联本身并让对象保持单独关联?

How do I destroy the association itself and leave the objects being associated alone, while keeping this RESTful?

具体来说,我有以下模型:

Specifically, I have these models:

class Event < ActiveRecord::Base
  has_many :model_surveys, :as => :surveyable, :dependent => :destroy, :include => :survey
  has_many :surveys, :through => :model_surveys
end

class ModelSurvey < ActiveRecord::Base
  belongs_to :survey
  belongs_to :surveyable, :polymorphic => true
end

class Survey < ActiveRecord::Base
  has_many :model_surveys
end

这表示事件为:surveyable(ModelSurvey belongs_to Event).我的问题是,不必创建ModelSurveysController,如何在不使用EventSurvey的情况下销毁ModelSurvey?

That's saying that the Event is :surveyable (ModelSurvey belongs_to Event). My question is, without having to create a ModelSurveysController, how do I destroy the ModelSurvey, while leaving the Event and Survey alone?

map.resources :events, :has_many => :model_surveys有什么问题吗?我不太确定在这种情况下该怎么办.路由需要发生什么,控制器中需要发生什么?我希望网址看起来像这样:

Something with map.resources :events, :has_many => :model_surveys? I'm not quite sure what to do in this situation. What needs to happen with the routes, and what needs to happen in the controller? I'm hoping the url could look something like this:

/events/:title/model_surveys/:id

感谢您的帮助, 兰斯

推荐答案

在Rails 2.3中,您具有accepts_nested_attributes_for,它可以让您将ModelSurveys数组传递给相关事件.如果您允许通过嵌套属性声明进行销毁,则可以传递event[model_surveys][1][_destroy]=1,关联将被删除.查看 api文档.

In Rails 2.3 you have accepts_nested_attributes_for which would let you pass an array of ModelSurveys to the event in question. If you allow destroy through the nested attributes declaration, you'll be able to pass event[model_surveys][1][_destroy]=1 and the association will be removed. Check out the api docs.

这篇关于RESTful破坏Rails中的多态关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆