Rails, running becomes after a find -
i need change class of each result returned in active record query. to happen whenever query run , results returned. unfortunately cannot use sti accomplish this. example:
class foo < activerecord::base after_find :becomes_a_subclass def becomes_a_subclass becomes [bar, baz, raz].sample end end class bar < foo ; end class baz < foo ; end class raz < foo ; end
i expect following return given result:
=> foo.all [#<bar id: 1>, #<baz id: 2>, #<bar id: 3>, #<raz id: 4>]
you'll want implement discriminate_class_for_record(record)
class method on foo
. should return subclass record. example implementation:
class foo < activerecord::base def self.discriminate_class_for_record(record) if record['foobar'] [bar, baz, raz].sample else super end end end class bar < foo ; end class baz < foo ; end class raz < foo ; end
Comments
Post a Comment