ruby on rails - How to update thousands of records -


i have update age column based on value in date of birth column. there thousands of records update.

how do using rails?

is right way it?

user.update_all(:age => some_method);  def some_method   age = date.today.year - dob.year end 

yes, update_all right method no, can't this. some_method called once set database call (i assume you're persisting database). you'll error because dob won't recognised in scope of user class.

you'll need translate date logic sql functions.

something (for mysql):

user.update_all("age = year(now()) -                  year(dob) -                  (date_format(now(), '%m%d') < date_format(dob, '%m%d'))") 

(nb. date_format stuff right age people who's birthdays later in year current date - see this question more details)


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -