Sha256: 3f37d74ebef325904d0ad49607d52453e24d98ebae41d5f195c2162159555aac

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 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 ::ArqlModel
      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

3 entries across 3 versions & 1 rubygems

Version Path
arql-0.4.0 auto-set-id-before-save.org
arql-0.3.31 auto-set-id-before-save.org
arql-0.3.30 auto-set-id-before-save.org