Restart Webrick when mangling constants with Merge! (of course!)

Yesterday I've encountered the irritating date problem in Rails. Please remember to restart Webrick when you change the date conversion constants or time conversion constants with the 'merge!' method:

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(
:default => "%d-%m-%Y"
)

On reload it merges the array again... You can never remove the ':default' date with a merge...
*sigh*

This tip can save you a whole lot of time, because reloading doesn't work

Default Date Format Horrors in Rails

How nice of rails to make it easy to change the default date format that is used when
showing a date from the database.

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(
:default => "%d-%m-%Y"
)

Very easy. Simple writing:

model.date.to_s

outputs the date in my format.

All seems to work well, but a few days later. TODAY!! I found out my application wasn't saving dates anymore.

The cause:

Rails uses the ":default" date definition to format the date when it builds an SQL statement. Well my MySQL version doesn't seem to understand this date format.

For now I stay away from the :default date formatter.
I've changed my custom date_picker field so now it first formats the date with the date format before showing..

Btw. I found a possible solution and a remark that this has been solved for future rails version. You can find this info at, the rails date kit.

Ruby Constants – Note to Self ::

Well after trying to access a ruby class constant for the zillionth time with Class.CONSTNAME.
( Which btw. very nicely gives the error "Undefined Method Class.CONSTNAME"...)
I've decided to write this note on my blog...

Remember:

print Class::InnerClass::CONSTANT
print Class::InnerClass.method_name

* kick * to my own butt..

Great method to_json

A very nice method to convert a Ruby array to Javascript.

print { 'key1' =>  'value1', 'key2' => [ 1,  2, 3 ] }.to_json
outputs:  { key1: 'value1', key1: [ 1, 2, 3 ] }

Rails: unit test fixture_path NameError.

Last day of my Holiday :(
Trying to write a unit test for my Rails application, I constantly got the error message below:


NameError: undefined local variable or method `fixture_path' for #<LessonTest:0x9a611f8>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/test_process.rb:452:in `method_missing'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:593:in `load_fixtures'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:538:in `setup_with_fixtures'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:575:in `setup'

Very strange: the Unit Test I've written was generated and exactly the same as shown in the example in the "Agile Web Development with Rails 2nd Edition" book.


require File.dirname(__FILE__) + '/../test_helper'
class LessonTest < Test::Unit::TestCase
def test_truth
assert true
end
end

Well I found a solution, but I still haven't found the reason WHY this is happening. You can see this is as a temporary hack.

In your: "/test/test_helper.rb" add the following method:


def fixture_path
File.dirname(__FILE__) + "/fixtures/"
end