ruby - Defining method_missing on Active record in rails 4 throws SystemStackError: stack level too deep on attributes -


i upgraded app rails 4.0 , ruby 2.0 i'm having problems understanding why method_missing definitions won't work. i'm pretty sure i'm not doing differently before.

specifically, i'm trying create method lets activerecord object respond call object belongs_to via polymorphic relationship.

here classes:

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 

i want able call event.song when eventable_type of event == 'song' issue on self.eventable_type, triggers stack overflow.

what missing here?

it seems though eventable_type method isn't yet defined when method_missing triggers (some methods in rails dynamically defined through method_missing first time call them).

i'd try different means of getting value want; perhaps self.attributes["eventable_type"] work?


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -