2

私はポリモーフィック アソシエーションの問題に取り組んでいますが、どこに行けばよいのかよくわかりません。

これが私の現在のモデル設定です:

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 関係を実行しようとしていないため、問題がどこにあるのかわかりません。

4

1 に答える 1

0

問題はパーツ モデルにあります。has_oneポリモーフィックを通じて関連付けがありますbelongs_to

私はまだあなたに答えを持っていません... しかし、それはあなたの質問に答えます. 私はそれがラベル付けされた定数であるか、私たちのような彼らがしたくない/できないことをしようとしている人のために再利用しているだけだと思います.

partable表面的には、DB に列がないためだと思いますがpartable_idpartable_type.

于 2013-01-01T15:25:52.403 に答える