Rails formatting date

Use Model.created_at.strftime(“%FT%T”) where, %F – The ISO 8601 date format (%Y-%m-%d) %T – 24-hour time (%H:%M:%S) Following are some of the frequently used useful list of Date and Time formats that you could specify in strftime method: Date (Year, Month, Day): %Y – Year with century (can be negative, 4 digits at least) -0001, 0000, … Read more

How to change default timezone for Active Record in Rails?

I have decided to compile this answer because all others seem to be incomplete. config.active_record.default_timezone determines whether to use Time.local (if set to :local) or Time.utc (if set to :utc) when pulling dates and times from the database. The default is :utc. http://guides.rubyonrails.org/configuring.html If you want to change Rails timezone, but continue to have Active … Read more

Rails – Strong Parameters – Nested Objects

As odd as it sound when you want to permit nested attributes you do specify the attributes of nested object within an array. In your case it would be Update as suggested by @RafaelOliveira params.require(:measurement) .permit(:name, :groundtruth => [:type, :coordinates => []]) On the other hand if you want nested of multiple objects then you … Read more

How can I avoid running ActiveRecord callbacks?

Use update_column (Rails >= v3.1) or update_columns (Rails >= 4.0) to skip callbacks and validations. Also with these methods, updated_at is not updated. #Rails >= v3.1 only @person.update_column(:some_attribute, ‘value’) #Rails >= v4.0 only @person.update_columns(attributes) http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update_column #2: Skipping callbacks that also works while creating an object class Person < ActiveRecord::Base attr_accessor :skip_some_callbacks before_validation :do_something after_validation :do_something_else … Read more