Find age against the date

Hi,
basically, I just want to explain my question,
there is a column named as “age” in my database.
the data structure is like this “1980/2/2”.
my question is, I have got two textboxes like this

<%= text_field 'min_age', 'min_age' %>

<%= text_field 'max_age', 'max_age' %>

.

<%= submit_tag 'Search' %>

and the input is like this; min_age= "20" and max_age = "30"

so, I want to search the age groups(20~30, 30~50 etc.) from the age
column.

how can I do that? please help.

a column named as “age” in my database

What is the type of this column? If you have a string then it’s quite
complicated to perform this math;

Emanuele T. wrote:

a column named as “age” in my database

What is the type of this column? If you have a string then it’s quite
complicated to perform this math;

ahh! sorry. column type is date.

Robert W. wrote:

Ahmet K. wrote:

and the input is like this; min_age= “20” and max_age = “30”

ahh! sorry. column type is date.

If you’re column type is data and name age and you want to store values
like “20” and “30” then what you’re staying doesn’t make any sense to
me.

I do not want to store values 20 and 30, I want to search by 20 and 30
etc.
My stored date is date format and like this 2000/3/3.

Here’s what would make sense:

  1. Store the person’s date of birth in a date field
  2. Add a method to the Person class to calculate age from date of birth.
  3. Given that you need to perform a range based query on age you need to
    calculate the date of birth range that would provide you the age range
    based on the current date.

Example:

calculate the date of birth for min_age based on current date.

start_dob = dob_for_age(params[:min_age])

calculate the date of birth for max_age based on current date.

end_dob = dob_for_age(params[:max_age])

Find people based for age range using date of birth

@people = Person.find(:all, :conditions => { :date_of_birth =>
start_dob…end_dob })

Ahmet K. wrote:

and the input is like this; min_age= “20” and max_age = “30”

ahh! sorry. column type is date.

If you’re column type is data and name age and you want to store values
like “20” and “30” then what you’re staying doesn’t make any sense to
me.

Here’s what would make sense:

  1. Store the person’s date of birth in a date field
  2. Add a method to the Person class to calculate age from date of birth.
  3. Given that you need to perform a range based query on age you need to
    calculate the date of birth range that would provide you the age range
    based on the current date.

Example:

calculate the date of birth for min_age based on current date.

start_dob = dob_for_age(params[:min_age])

calculate the date of birth for max_age based on current date.

end_dob = dob_for_age(params[:max_age])

Find people based for age range using date of birth

@people = Person.find(:all, :conditions => { :date_of_birth =>
start_dob…end_dob })

some progress,

I am writing like this
sql += " and ADDDATE(birthday_date, INTERVAL #{min_age} YEAR) <
CURDATE()"
but gave me this error
wrong number of bind variables : users.deleted = 0 and fullname like ?
and ADDDATE(birthday, INTERVAL 20 YEAR) < CURDATE()

please help

Ahmet K. wrote:

some progress,

I am writing like this
sql += " and ADDDATE(birthday_date, INTERVAL #{min_age} YEAR) <
CURDATE()"
but gave me this error
wrong number of bind variables : users.deleted = 0 and fullname like ?
and ADDDATE(birthday, INTERVAL 20 YEAR) < CURDATE()

please help

I am updating my problem:

my progress like this,

if !(max_age = session[:user_search][:max_age]).blank?
max_age = Time.now-params[:max_age].to_i.years
smax_age = max_age.strftime("%Y/%m/%d")
sql +=" and birthday_date <= ? "
param << “%#{smax_age}%”
end

and the result is : and birthday_date >= ‘%1989/11/24%’

so my problem is, I want to get this result ‘1989/11/24’,not this
result ‘%1989/11/24%’.
How can I alter this problem.

Ahmet K. wrote:

my progress like this,

if !(max_age = session[:user_search][:max_age]).blank?
max_age = Time.now-params[:max_age].to_i.years
smax_age = max_age.strftime("%Y/%m/%d")
sql +=" and birthday_date <= ? "
param << “%#{smax_age}%”
end

and the result is : and birthday_date >= ‘%1989/11/24%’

so my problem is, I want to get this result ‘1989/11/24’,not this
result ‘%1989/11/24%’.
How can I alter this problem.

First of all DO NOT use strings to store date values. Use an actual date
column type. Database are very good a dealing with actual date values
these days. This will make you life much easier.

You’re making this too difficult on yourself. If you actually read my
first reply I gave you one possible solution. All you need is a method
that takes the age and gives you the date of birth that matches the
given age based on today’s date.

calculate the date of birth for min_age based on current date.

start_dob = dob_for_age(params[:min_age])

Use this method to calculate the first date that would make the person
20 years old (per your example). then use the same method to calculate
the date that would make the person 30 years old. Now you can write a
very simple date range query (per my original example):

Find people based for age range using date of birth

@people = Person.find(:all, :conditions => { :date_of_birth =>
start_dob…end_dob })

You can put the dob_for_age method wherever it make sense to you. I
think I’d just add it as a class method on Person so I could call
Person.dob_for_age(params[:age]) from the controller:

class Person << ActiveRecord::Base
def self dob_for_age(age)
# Calculate and return the first date for given age
end
end

Or you could even have write the method to return the date range in a
single call:

class Person << ActiveRecord::Base
def self dob_range_for_age_range(age_range)
# Calculate and return the date range for given age range
end
end