ruby on rails - custom method_missing ignored if instance is defined via an ActiveRecord association -
i have submissions might in various states , wrote method_missing override allows me check state calls
submission.draft? submission.published?
this works wonderfully.
i also, various reasons might not great, have model called packlet
belongs_to meeting , belongs_to submission. however, surprised find that
packlet.submission.draft?
returns nomethoderror
. on other hand, if hard-code #draft?
method submission
, above method call works.
how method_missing methods recognized when instance defined via activerecord association?
have added draft? method respond_to? method object? guess issue might arise there. happens when type:
submission.respond_to?(:draft?)
to fix this, write respond_to? method this:
def respond_to?(method, include_priv = false) #:nodoc: case method.to_sym when :draft?, :published? true else super(method, include_priv) end end
my recommendation implement without using method_missing instead though, doing meta-programming this:
class submission [:draft, :published].each |status| define_method "#{status}?" status == "#{status}?" end end end
Comments
Post a Comment