Useful Tips for Ruby on Rails Beginner


This article describes some useful tips for Ruby on Rails.

🍮 Time

ISO8601 with micro second

To get the current time with ISO 8601 format with microseconds is as follows:

Time.current.iso8601(6)
=> "2016-07-01T13:03:00.193967+09:00"

🍣 Concern

  • Define scope in include block
  • Module definition class method in block of ClassMethods
  • Instance method is directly below module
module MyConcern
extend ActiveSupport::Concern

included do
scope :without_deleted, lambda{ where(deleted_at: nil) }
end

module ClassMethods
def foo
puts 'foo'
end
end

def bar
puts 'bar'
end
end

[Troubleshooting] Undefined method model_name for Project:Class

module MyConcern
extend ActiveModel::Naming
# ...
end

🖥 Recommended VPS Service

VULTR provides high performance cloud compute environment for you. Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour). In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results!!