title: Default value for a float, based on related model in mongoid
body: <p>I have an app, where users list can be filtered on some criteria. One such criteria is experience of the user an mentioned in professional details. I have different kind of filters at different places in app and have a model corresponding to each filter. some of them have common functionality. So I extracted the common portion in a module, which can be included in any model to get the required functionality. I am trying to follow the concept of Single Responsibility here.</p>
  
  <pre><code># model
  class Filter::WorkOpportunity
    include Mongoid::Document
    include Filter::NotificationSubFilter
    include Filter::User::Qualification
    include Filter::User::Experience
  
    belongs_to :college
  
    def matched_user_ids
      # ...
    end
  end
  
  # one of the included module I am having problems with
  # others work fine
  module Filter::User::Experience
    extend ActiveSupport::Concern
    DEFAULT_MAX_EXP = 10.0
  
    included do
      include InstanceMethods
      field :min_exp, :type =&gt; Float, :default =&gt; 0.0
      field :max_exp, :type =&gt; Float, :default =&gt; lambda { default_max_exp }
    end
  
    module InstanceMethods
      # Appends users criteria passed, to filter on qualifications
      def filter_experience users=nil
        # ...
      end
  
      def default_max_exp
        @default_max_exp ||= begin
          established_at = self.college.try(:established_at)
          if established_at.blank?
            DEFAULT_MAX_EXP
          else
            [(Time.now.year - established_at.year + 1), DEFAULT_MAX_EXP].max.to_f
          end
        end
      end
    end
  end    
  </code></pre>
  
  <p>I get an error when I try to initialize the filter. </p>
  
  <pre><code>NameError: undefined local variable or method `default_max_exp' for Filter::WorkOpportunity:Class
  </code></pre>
  
  <p>Other included modules have similar structure. Even if I move the <code>default_max_exp</code> to the <code>included</code> block, it still doesn't work. </p>
  
  <p>While I was posting the question here I realized that it is because, the <code>default_max_exp</code> should be a class method, and it is an instance method. But I want the default value to instance based, as the different filters can belong to different colleges and default maximum experience should be based on college.</p>
  
  <p>How can I have an instance based default?</p>
  
  <p>RoR: 3.0.4, Mongoid: 2.0.2</p>