Rails 3 MySQL query question -


my job model has id , percentage fields (among others). percentage may nil.

i set default percentage here:

class jobscontroller   def new     ...     @job.percentage = <what should here?>     ...   end end 

it should calculated this:

  • take percentage of latest job (a job max id), percentage isn't nil.
  • if there no jobs, or jobs percentage nil, should 23.

what easiest method calculate ?

you should in job model:

[update]

added test new_record? in callback, since percentage can legitimately nil; don't want override if it's been set such.

class job < activerecord::base   after_initialize :calculate_default_percent    # other stuff    private      def calculate_default_percent       if self.new_record?         conditions = { :conditions => "percentage not null" }         self.percentage = job.last(conditions).nil? ? 23 : job.last(conditions).percentage       end     end end 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -