简体   繁体   English

Rails 3 多态关联问题

[英]Rails 3 Polymorphic Associations Question

I've been trying to switch my Orders model to a polymorphic association with my Product and Service models.我一直在尝试将我的Orders model 切换为与我的ProductService模型的多态关联。 However, I have a few questions that I haven't been able to find answers to, even after watching the RailsCast and reading the documentation (so, those suggestions are appreciated, but I need a more concrete answer).但是,即使在观看 RailsCast 并阅读文档之后,我也有一些问题无法找到答案(因此,这些建议值得赞赏,但我需要更具体的答案)。

Question:问题:

  1. Is a polymorphic association the best thing to use in this case?在这种情况下,多态关联是最好的吗? Prior to this, I was using a Transaction model that had multiple belongs_to associations and used a custom Parent function to determine which one it was.在此之前,我使用了一个Transaction model,它有多个belongs_to关联,并使用了一个自定义 Parent function 来确定它是哪一个。 This was working fine, but someone suggested a polymorphic association may clean things up.这工作正常,但有人建议多态关联可以清理事情。
  2. I set up the polymorphic association properly and have been unable to have the transactable_id and transactable_type automatically populated.我正确设置了多态关联,并且无法自动填充transactable_idtransactable_type The code is below.代码如下。 I have side-stepped this by manually putting them in inside the form, but if anyone knows the proper way to do it, that would be great!我已经通过手动将它们放入表单中来避开这一点,但是如果有人知道正确的方法,那就太好了!
  3. How can I access elements with polymorphic associations?如何访问具有多态关联的元素? For example, in my Cart object (which has_many Transactions and which Transactions belongs_to ) I can no longer access things using @cart.transactions.each do |t|... @t.product.name type coding.例如,在我的购物车 object(其中has_many Transactions 和 which Transactions belongs_to )中,我无法再使用@cart.transactions.each do |t|... @t.product.name类型编码。

My model associations look like this:我的 model 关联如下所示:

class Order < ActiveRecord::Base
  belongs_to :orderable, :polymorphic => true
end

class Product < ActiveRecord::Base
  has_many :orders, :as => :orderable
end

My forms used to look like this:我的 forms 曾经看起来像这样:

<% form_for [@orderable, @order] do |f| %>
  ...
<% end %>

And were rendered like this in my Product Show view:并在我的Product展示视图中呈现如下:

<%= render 'orders/form' %>

Now, I pass a variable for the product.id in the render partial and use it to populate the transactable_id field.现在,我在渲染部分中为 product.id 传递一个变量,并使用它来填充transactable_id字段。 But, I feel like that is very messy.但是,我觉得这很混乱。

Again, I have read the tutorials and API docs and have been unable to solve this, so any help would be greatly appreciated!!同样,我已经阅读了教程和 API 文档并且无法解决这个问题,所以非常感谢任何帮助!

Answers to your questions:回答您的问题:

  1. If your business login implies that multiple models will have related model with the same fields so you should use polymorphic association.如果您的业务登录意味着多个模型将具有相关的 model 具有相同的字段,那么您应该使用多态关联。 (In your case you can use it). (在您的情况下,您可以使用它)。
  2. If set up polymorphic association Rails will automatically handle setting *_id and *_type fields depending on associated parent model.如果设置多态关联,Rails 将根据关联的父 model 自动处理设置*_id*_type字段。
  3. Lets say you have Product with many orders in polymorphic association and you want to define which model order belongs to:假设您的 Product 具有多态关联中的许多订单,并且您想定义 model 订单属于哪个:

     order = Order.first order.orderable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM