2013-04-18 23 views
3

我最近將應用程序升級到了rails 4.0和ruby 2.0 我在理解爲什麼我的method_missing定義不起作用。我很確定我沒有做任何與以前不同的事情。在rails 4的Active記錄中定義method_missing拋出SystemStackError:屬性的堆棧層次太深

具體來說,我想創建一個方法,讓一個ActiveRecord對象通過多態關係來響應對一個對象的調用它belongs_to

這裏是我的課:

song.rb

class Song < ActiveRecord::Base 
    has_many :events, :as => :eventable 
end 

event.rb

class Event < ActiveRecord::Base 

    belongs_to :eventable, :polymorphic => true 

    def method_missing(meth, *args, &block) 
     if meth.to_s == self.eventable_type 
      self.eventable 
     else 
      super 
     end 
    end 

end 

我希望能夠調用event.song當事件==「宋eventable_type ' 問題出在self.eventable_type上,這會觸發堆棧溢出。

我在這裏錯過了什麼?

回答

1

它好像當method_missing觸發器(在Rails的一些方法打通method_missing你第一次給他們打電話動態定義)eventable_type方法尚未確定。

我想嘗試一種不同的方式來獲得你想要的值;也許self.attributes["eventable_type"]將工作?

+0

你,先生,是我今天的個人英雄。 – rainbowFish

相關問題