1

The User needs to input his birthday before saving his profile. I also have an age column in the profiles table. But I don't want the user to have to fill in his DOB and age. I already have a method that calculates the users age based on his birthday. The question is how do I save the users age in the database once calculated?

pratski
  • 1,448
  • 2
  • 22
  • 37

5 Answers5

5

I would recommend that you don't save it in the database, calculate it when it's needed in your application.

If you save it in the DB then you risk them getting older without the value being updated!

muttonlamb
  • 6,341
  • 3
  • 26
  • 35
  • If i want to run a query to find members between an age range, wouldnt it be easier to have an age column? As opposed to running a query on the dob column? – pratski Mar 20 '13 at 09:55
  • It's a very simple query that shouldn't cause any big issues unless you're doing it for 100,000's constantly. – muttonlamb Mar 20 '13 at 11:22
2

You can have an abstract attribute in your model (not a column in your database), which will calculate the age for you. For example:

dob = column in your database table
age = abstract attribute

class User < ActiveRecord::Base
  attr_accessible :dob
  attr_accessor :age

  def age
    Time.now.year - self.dob.year
  end

end

In this way, you can easily find the age of an user:

u = User.find(1)
u.dob
=> Thu, 20 Mar 2008 
u.age
=> 5 

However, I recommend you to take a look on this post as there are some issues regarding leap years, and you might want to adjust your age method.

EDIT - Anyway, if you have a really good reason to add the column age in your table, you should use before_save as it will be fired on create and update.

class User < ActiveRecord::Base
  attr_accessible :dob, :age
  before_save :set_age

  def set_age
    self.age = Time.now.year - self.dob.year
  end

end
Community
  • 1
  • 1
gabrielhilal
  • 10,660
  • 6
  • 54
  • 81
  • Excellent, I implemented the first part of your answere by creating an age method (after reading the post you mentioned) and have a pretty handy age method. Now I can easily call @user.age! – pratski Mar 20 '13 at 13:16
  • Seeing a lot of recommendations that OP does not save in db. I think that's definitely true of this particular answer, since they need a computed property which depends entirely on a single column. The golden rule is, if you have a property which is derived from 2 or more columns (e.g. time_since_joined), then it probably DOES make sense to use the before_save and a database column so that you can sort and filter by this property. Just wanted to add my two cents, because computing on get can be disastrous for performance if you need filtering/sorting. – hedgehogrider Apr 17 '20 at 17:56
1

In your application at HTML page by using java-script function you can calculate the age of user by jquery - date method.

Just write a function calculate_age() in JS file. and put that value in one of the hidden field. You can use that hidden field for storing the age in db.

At that input box where you are collecting date of birth, just write onblur event to call calculate_age() function.

HTML Code

<input type='text' id='dob' name='dob' onblur='calculate_age(this);' />
<input type='hidden' id='age' name='age' />

JS Code

function calculate_age(element_id){
    var date_of_birth = $('#'+element_id).val();
   // JS code for calculating age using date of birth
   $('#age').val(age);
}
Rubyist
  • 6,486
  • 10
  • 51
  • 86
1
class User < ActiveRecord::Base

before_create :calculate_age



def calculate_age
   self.age= Time.now - self.birthday # do calculate yourself, I don't know your parameters
end
user1619685
  • 121
  • 8
0

You do need to save it. Age should be calculated at run time from DOB column.

Umair Anwar
  • 522
  • 1
  • 4
  • 15