Restkit,RubyOnRails,iOS Dev
28 十月 2011 | View Comments
If you are using Rails as your backend, Reskit will have trouble to parse ActiveRecord timestamp.
Default ActiveRecord timestamp JSON out is 2008-12-29T00:27:42-08:00
You can not convert it to NSDate directly use NSDateFomatter
you must convert it to 2008-12-29T00:27:42-0800
But you have not chance to convert when you use Restkit Mapping.
Here is my solution
1. Monkey patch Time and DateTime class
class Time
def as_json(option=nil)
self.strftime("%Y-%m-%dT%H:%m:%S%z")
end
end
class DateTime
def as_json(format=nil)
self.strftime("%Y-%m-%dT%H:%m:%S%z")
end
end
2. Add default date fomatter to RKObjectMapping
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
[RKObjectMapping addDefaultDateFormatter:dateFormatter];
Tagged in activerecord, NSDateFomatter, Restkit
RubyOnRails
15 二月 2010 | View Comments
Today I wrote an ActiveRecord initialize method like this
def initialize
@aa = "aa"
end
Then this error occurred.
NoMethodError: You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.has_key?
Very confuse.
After I saw source of ActiveRecord, I realized I override ActiveRecord default initialize method by mistake.
If you really want to do this. There two ways:
def initialize(params = nil)
super
# do something else
end
More graceful
def after_initialize
@aa = "aa"
end
Tagged in activerecord, RubyOnRails
RubyOnRails,Tips
15 二月 2010 | View Comments
before
Account.find(:all, :conditions => ['name LIKE ? AND updated_at < ?', "aname", 3.days.ago])
After
Account.find(:all, :conditions => ['name LIKE :name AND updated_at < :date', {:name => "aname", :date => 3.days.ago}])
Tagged in activerecord, RubyOnRails
RubyOnRails,Tips
15 二月 2010 | View Comments
Item.find( :all, :select => 'DISTINCT fieldname' )
Tagged in activerecord, mysql