Skip to content

Instantly share code, notes, and snippets.

@TheLarkInn
Created September 19, 2013 15:26
Show Gist options
  • Save TheLarkInn/6625175 to your computer and use it in GitHub Desktop.
Save TheLarkInn/6625175 to your computer and use it in GitHub Desktop.
Way to create models for all tables in a MySQL database using Sequel, and Sequel's Sequel::Model() command.
require 'sequel'
DB = Sequel.connect('mysql://root@localhost/macpractice')
tables_array = DB["show tables"].all.map {|x| x[:"Tables_in_macpractice"]}
tables_array.each {|table_name| instance_variable_set("@#{table_name}", Sequel::Model(table_name.to_sym))}
@TheLarkInn
Copy link
Author

Classes are created for each table with the name "@table_name_goes_here".

1.9.3-p429 :019 > @iform_field_type
=> #Class:0x007f9f9b515c60

1.9.3-p429 :020 > @iform_field_type[3]
=> #< @values={:iform_field_type_id=>3, :type_name=>"Popover"}>

1.9.3-p429 :021 > @iform_field_type.all
=> [#< @values={:iform_field_type_id=>1, :type_name=>"TextField"}>, #< @values={:iform_field_type_id=>2, :type_name=>"TextView"}>, #< @values={:iform_field_type_id=>3, :type_name=>"Popover"}>, #< @values={:iform_field_type_id=>4, :type_name=>"DatePicker"}>, #< @values={:iform_field_type_id=>5, :type_name=>"Checkbox"}>, #< @values={:iform_field_type_id=>6, :type_name=>"CheckboxText"}>, #< @values={:iform_field_type_id=>7, :type_name=>"Label"}>, #< @values={:iform_field_type_id=>8, :type_name=>"Slider"}>, #< @values={:iform_field_type_id=>9, :type_name=>"Drawing"}>, #< @values={:iform_field_type_id=>10, :type_name=>"List"}>, #< @values={:iform_field_type_id=>11, :type_name=>"SegmentedControl"}>, #< @values={:iform_field_type_id=>12, :type_name=>"Text Block"}>]

1.9.3-p429 :031 > @iform_field_type.first.type_name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment