মঙ্গলবার, ১৩ অক্টোবর, ২০০৯

Date difference in rails

when we are working on rails, sometimes we may require this

Say, we have User model and we need to find out how many days he registered.
You can have lots of ways to find that as you are on Rails(some might have some problem though)

here is another way(I think it will work smartly )


user = User.find(#id)


"Registered #{user.created_at.to_date - Date.today).to_i} days ago"

Similarly for days difference between two days :

(user1.created_at.to_date - user2.created_at.to_date).to_i

Cheers!