Rails 中的多态关联
如果我有一个属于多态关联(建议)的模型(选择),是否可以以选择的形式创建关联的建议?
本质上,我想做的是允许用户在民意调查中创建一个“选择”,潜在的建议是预定义的场地、城市或地区,而且还允许自定义自由格式建议的选项(仅文本) ,而不是我的数据库中存在的预定义实体)。对于场地/城市/地区,很简单,只需为用户提供一个选择选项并将选择与其关联,但是 CustomSuggestion 是否可以动态创建它并将选择与其关联?
我在想,我真正想要的是关联为 has_one CustomSuggestion,以允许关联构建,但这将是与 isn't_to :suggestion, :polymorphic => 不同的关联。真的。
有什么想法吗?
谢谢, 埃里克
If I have a model (Choice) belonging to a polymorphic association (Suggestion), is it possible to create the associated Suggestion in a form for the Choice?
Essentially, what I'm trying to do is allow a user to create a "Choice" in a poll, with potential suggestions being predefined Venues, Cities, or Districts, but also to allow an option for custom free-form suggestion (just text, and not a predefined entity existing in my database). For Venues/Cities/Districts, it's simple enough to just give the user a selection option and associate the Choice with it, but is it possible for a CustomSuggestion to create it on the fly and associate the Choice with it?
I was thinking that what I really want is the association to be has_one CustomSuggestion, to allow for association building, but then that would be a different association from the belongs_to :suggestion, :polymorphic => true.
Any ideas?
Thanks,
Eric
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我过去曾这样做过,并且没有为其复杂的对象模型所困扰。例如,在一份工作申请表中,我有一个“你是如何听说我们的问题?”如果您填写了该框,我会将其视为已填写的答案。因此,我有一个“belongs_to”指向正常选择,但只是模型上的一个文本字段用于自定义选择。
当有多个人提出相同的建议时,您的建议表会看起来很奇怪。一般来说,我质疑什么时候将某些东西建模为“has_one”。
I have done this in the past and I didn't bother with a complex object model for it. For example, in a job application form, I had a "how did you hear of us question?" If you filled in the box, I took that as a filled in answer. So, I had a "belongs_to" that pointed to the normal choices, but just a text field on the model for a custom one.
Your suggestions table is going to look weird when there are multiple people with the same suggestion. In general, I question when something is modeled as a "has_one".