フォーク、ActiveRecord、has_many:through、および多態性の関連付け
これを正しく理解してもらいたいですか?そしてここで継承のためのケース(SentientBeing)を無視して、代わりにhas_many:の多型モデルに焦点を当ててみてください。私が行うとき、しかし
widget.people << my_person
:それは、完璧な世界では、次の...
class Widget < ActiveRecord::Base
has_many :widget_groupings
has_many :people, :through => :widget_groupings, :source => :person, :conditions => "widget_groupings.grouper_type = 'Person'"
has_many :aliens, :through => :widget_groupings, :source => :alien, :conditions => "video_groupings.grouper_type = 'Alien'"
end
class Person < ActiveRecord::Base
has_many :widget_groupings, :as => grouper
has_many :widgets, :through => :widget_groupings
end
class Alien < ActiveRecord::Base
has_many :widget_groupings, :as => grouper
has_many :widgets, :through => :widget_groupings
end
class WidgetGrouping < ActiveRecord::Base
belongs_to :widget
belongs_to :grouper, :polymorphic => true
end
を考えると、私は、ウィジェットと人与えられた、のような何かをしたいと思いますこれは、私は 'grouper'の 'タイプ'が常にwidget_groupingsでnullであることに気づいた。しかし、私は次のようなものに:
widget.widget_groupings << WidgetGrouping.new({:widget => self, :person => my_person})
私は通常予想通りに動作します。私はこれが非多態的な関連で起こっているとは思っておらず、これがこのユースケースに特有のものかどうか、あるいはもし私が潜在的にバグを見つめているのかどうかを知りたいと思っていました。
ありがとうございました!
ああ私の神はとても痛いほど明白だ。感謝EmFi! – Cory
問題はありませんが、私は初めてこれに遭遇したときに、これをどうやって行うのかについて約一日苦労したと思います。私がRailsでやってみた最初のことの1つであるチュートリアル/ブックに従わないことを助けてくれませんでした。 – EmFi
EmFiありがとうございます。 – jdl