第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

ActiveRecord,has_many:through和多態(tài)關(guān)聯(lián)

ActiveRecord,has_many:through和多態(tài)關(guān)聯(lián)

慕標(biāo)5832272 2019-12-09 09:46:17
民間,想要確保我正確理解了這一點。并且請忽略此處的繼承情況(SentientBeing),而嘗試著重于has_many:through關(guān)系中的多態(tài)模型。也就是說,請考慮以下事項...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'"endclass Person < ActiveRecord::Base  has_many :widget_groupings, :as => grouper  has_many :widgets, :through => :widget_groupingsendclass Alien < ActiveRecord::Base  has_many :widget_groupings, :as => grouper  has_many :widgets, :through => :widget_groupings  endclass WidgetGrouping < ActiveRecord::Base  belongs_to :widget  belongs_to :grouper, :polymorphic => trueend在一個完美的世界中,我想給一個小部件和一個Person,執(zhí)行以下操作:widget.people << my_person但是,當(dāng)我這樣做時,我注意到widget_groupings中“分組器”的“類型”始終為空。但是,如果我執(zhí)行以下操作:widget.widget_groupings << WidgetGrouping.new({:widget => self, :person => my_person}) 然后所有的工作都像我通常期望的那樣。我認為我從未見過這種情況發(fā)生在非多態(tài)關(guān)聯(lián)中,只是想知道這是否是該用例所特有的,或者我是否有可能盯著一個錯誤。謝謝你的幫助!
查看完整描述

3 回答

?
慕村9548890

TA貢獻1884條經(jīng)驗 獲得超4個贊

Rails 3.1.1的一個已知問題破壞了此功能。如果您首先遇到此問題,請嘗試升級,此問題已在3.1.2中修復(fù)。


你好親近 問題是您濫用了:source選項。:source應(yīng)該指向多態(tài)的belongs_to關(guān)系。然后,您要做的就是為要定義的關(guān)系指定:source_type。


對Widget模型的此修復(fù)應(yīng)允許您完全按照自己的意愿進行操作。


class Widget < ActiveRecord::Base

  has_many :widget_groupings


  has_many :people, :through => :widget_groupings, :source => :grouper, :source_type => 'Person'

  has_many :aliens, :through => :widget_groupings, :source => :grouper, :source_type => 'Alien'

end


查看完整回答
反對 回復(fù) 2019-12-09
?
慕神8447489

TA貢獻1780條經(jīng)驗 獲得超1個贊

有很多:through和多態(tài)不能一起使用。如果您嘗試直接訪問它們,它將引發(fā)錯誤。如果我沒記錯的話,您必須手寫widget.people和push例程。

我不認為這是一個錯誤,只是還沒有實現(xiàn)。我想我們會在功能中看到它,因為每個人都有可以使用它的情況。


查看完整回答
反對 回復(fù) 2019-12-09
  • 3 回答
  • 0 關(guān)注
  • 637 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號