Sha256: 05d6aec5a34b479eeebf9099b0d308a1ba141c3c8fc89858d43c04d9a8ca048f

Contents?: true

Size: 1.05 KB

Versions: 8

Compression:

Stored size: 1.05 KB

Contents

* Set ID automatically for new objects before saving

  For primary keys that are not =auto_increment=, you may want to set a unique ID for the object before saving it. You can do this:

  Create a file =~/.arql.d/auto_gen_id.rb= with the following content:

  #+BEGIN_SRC ruby
    class :Arql::BaseModel
      before_create do
        if id.blank?
          id_type = self.class.columns_hash['id'].sql_type.scan(/\w+/).first
          case id_type
          when 'bigint'
            self.id = ::Arql::ID.long
          when 'char'
            self.id = ::Arql::ID.uuid
          when 'varchar'
            self.id = ::Arql::ID.uuid
          end
        end
      end
    end
  #+END_SRC

  Then in =~/.arql.d/init.rb=, require this file:

  #+BEGIN_SRC ruby
    load(File.absolute_path(File.dirname(__FILE__) + "/auto_set_id.rb"))
  #+END_SRC

  The prerequisite is that the primary key of your table is the =id= field, and the type is one of =bigint=, =char= or =varchar=.

  If the primary key of your table is not the =id= field, you need to modify the code above.

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
arql-0.4.11 auto-set-id-before-save.org
arql-0.4.10 auto-set-id-before-save.org
arql-0.4.8 auto-set-id-before-save.org
arql-0.4.7 auto-set-id-before-save.org
arql-0.4.6 auto-set-id-before-save.org
arql-0.4.3 auto-set-id-before-save.org
arql-0.4.2 auto-set-id-before-save.org
arql-0.4.1 auto-set-id-before-save.org