多态关联,has_one和HasManyThroughAssociationPolymorphicThroughError

时间:2012-08-23 16:22:57

标签: ruby-on-rails-3.2 polymorphic-associations

我正在研究多态关联问题,而且我不太清楚该怎么做。

这是我目前的模型设置:

class Rfq < ActiveRecord::Base
    attr_accessible :name, :parts_attributes
    has_many :parts
    accepts_nested_attributes_for :parts
end

class Part < ActiveRecord::Base
    attr_accessible :name, :mailer_parts
    belongs_to :rfq
    belongs_to :partable, :polymorphic => true
    has_one :mailer, :through => :partable
    accepts_nested_attributes_for :mailer
end

class Mailer < ActiveRecord::Base
    attr_accessible :address
    has_one :part, :as => :partable  # Should/could this be belongs_to?
end

当我在浏览器中转到Rfq的编辑页面时,我看到了这个错误:

ActiveRecord::HasManyThroughAssociationPolymorphicThroughError in Rfqs#edit
Cannot have a has_many :through association 'Part#mailer' which goes through the polymorphic association 'Part#partable'.
Extracted source (around line #5):

2:   <%= f.label :name, "Name" %>
3:   <%= f.text_field :name %><br>
4:   <p>Part Information: </p>
5:   <%= f.fields_for :mailer do |builder| %>
6:     <%= render 'mailer_fields', :f => builder %>
7:   <% end %>
8:   <%= link_to 'Add Mailer Part', '#', :class => "add_mailer_part" %>

我不确定问题出在哪里,因为我没有尝试在任何地方建立has_many关系。

1 个答案:

答案 0 :(得分:0)

问题在于Part模型。您通过多态has_one获得belongs_to关联。

我对你没有答案,但是......但这回答了你的问题。我认为它只是一个标记不好的常数或者他们重复用于像我们这样的人试图做他们不喜欢/不想要的东西。

从表面上看,我认为这是因为数据库没有partable列,而是partable_idpartable_type