Home Rails saving datetime to MySQL database
Reply: 2

Rails saving datetime to MySQL database

Rumpleteaser
1#
Rumpleteaser Published in 2010-09-14 04:00:30Z

I have a simple function within my model to set a completed datetime column in a database. this function worked until I tried to change the default rails format for datetime.

After reading up on the issues with mysql datetime format and rails I decided to take out the formating code I had in the environment.rb

It still does not save though. I want it to save the same way that created_at etc. save. I can then format the display of the date.

I have restarted the web server

Model:

def self.complete(id)
   update(id, :completed_at => Time.now)
end

Controller:

Class.complete(params[:id])
Yannis
2#
Yannis Reply to 2010-09-14 07:02:08Z

Since you want to update a specific instance of your class (a specific record), you may switch from a class method to an instance method.

In your model:

def complete
  update_attribute(:completed_at, Time.now)
end

and in your controller:

def your_method
  @object = YourModel.find(params[:id])
  @object.complete
end
vise
3#
vise Reply to 2010-09-14 06:56:09Z

You could try:

update(id, :completed_at => Time.now.to_s(:db))
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.677269 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO