Posted by
Moser on June 23, 2009
A little gotcha with custom setters for datetime attributes is, that when setting an attribute through ‘write_attribute’ it is not converted to UTC (or whatever else your default time zone is).
This problem can easily be reproduced:
./script/generate model Thing a:datetime b:datetime
My model:
class Thing < ActiveRecord::Base
def b=(d)
write_attribute(:b, d)
end
end
My tests:
require 'test_helper'
class ThingTest < ActiveSupport::TestCase
test "a and b" do
d = DateTime.now
t = Thing.new
t.a = d
t.b = d
t.save
assert_equal t.a, t.b
t.reload
assert_equal t.a, t.b
end
end
The second assertion will fail. Is this intended or a bug?
Quick fix:
def b=(d)
d = d.utc
write_attribute(:b, d)
end
Posted by
Moser on June 18, 2009
I included jQuery by installing the jRails plugin, which seems nice because you can continue using rails’ ajax helpers. But when I tried to implement a more special functionality by using the $.ajax function it proved impossible to set the Accept header for my request. Neither using the dataType option nor by setting it directly in a beforeSend function. The Accept header always read:
text/javascript, text/html, application/xml, text/xml, */*
I was about to uninstall and hate jQuery for the rest of my life.
By coincidence I took a look at jrails.js which is a part of the jRails plugin:
(function($)
{
$().ajaxSend(
function(a,xhr,s){
xhr.setRequestHeader("Accept","text/javascript, text/html, application/xml, text/xml, */*")
}
)
}
)(jQuery);
[...]
WTF? Is that documented anywhere?
This hard coded shit stuff breaks rails’ respond_to functionality, doesn’t it?t
To be fair, it works if you use extensions to determine what datatype you expect. (Like /things/1.js)
But I don’t do that when I build a custom ajax request where I can set the Accept header directly.
Update:
Some research on the topic “accept header vs. extension” showed that in terms of cross browser compatibility I should favor the extension approach. And I’m not surprised it’s Microsoft’s fault
Posted by
Moser on June 17, 2009
It’s somehow trivial, but I found out about ‘write_attribute‘ only just: [another case of rtfm]
class Thing < ActiveRecord::Base
def name=(str)
str.upcase! unless str.nil?
write_attribute(:name, str)
end
end
Update:
Be careful if you use this with Datetime attributes.