Rhom vs. ActiveRecord, ID of Rhodes objects

rhodes-framework-mobile-developmentThe ID of an object (record in a table) is an important piece of information. In most of the situations when we need an object from the database, we search for it using its ID. Moreover, the ID is usually part of the URL of a page.

Given its importance it is worh mentioning a big difference between Rhom (the object mapper in Rhodes) and ActiveRecord (in Rails): in Rhom, the object ID is enclosed in brackets {}. A typical Rhom object ID looks like this: {32034644033132}.

Why is it such a big number?In order to keep things as fast and lightweight as possible, Rhodes team decided not to look at the current maximum object ID in a table and increase it by one (thus eliminating a database access). The solution they chosed was to start from the current time and build on that:

# line 374 in rhodes/lib/framework/rho/rho.rb
@@g_base_temp_id = nil
def generate_id
  @@g_base_temp_id = ((Time.now.to_f - Time.mktime(2009,"jan",1,0,0,0,0).to_f) * 10**6).to_i unless @@g_base_temp_id

  @@g_base_temp_id = @@g_base_temp_id + 1
  @@g_base_temp_id
end

Also, keep in mind that the name of the method is object, not id.

Rhom – ActiveRecord comparation:

Rhom ActiveRecord
form {32036182933314} 1
generation based on current time maxim id in table + 1
method name object id
URLs /User/{32036182933314}/edit /users/1/edit