Ruby on rails turn string into date and compare dates -


given following model:

class person    field :dob, type: string #in form of yyyy-mm-dd  end 

how write scope gets people have :dob values after, 2000-1-1? what's below:

scope :is_child, -> { where(:dob.to_date > "2000-1-1".to_date) } 

you're storing date iso8601 formatted string , iso8601 date strings compare sensibly (i.e. string comparison matches corresponding date comparison). means can use simple string operations:

scope :is_child, -> { where(:dob.gt => '2000-01-01') } 

you'd want use dynamic date based on current date instead of 2000-01-01 though, otherwise you'll have update code every year , that's error prone.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -