RailsInventory ApplicationSo, I was reading my emailRequirementsWhat we'll coverThe usualGeneration of the first MigrationWhat does this file do?1st Part2nd PartMigrationsWhat's the point?schema.rbPowerPoint PresentationBut…What rake really doesAbout the databaseSqlite3 syntaxSome sqlite3 commandsA Sample SessionYou might have noticedA blank migrationSlide 23Filling the empty migrationAvailable migration commandsUp and downRunning the MigrationResultsRolling backAn asideAn asideAfter all this…The errorEasy peasyCleaning upNow it worksValidationsSimple validationIncluded validationsOther Included ValidationsValidations added to the modelA rabbit holeAfter diggingBut where?A hackSlide 46Slide 47Works, but….Back to the beginningAn easy testA new migrationCustom ValidationsA new methodNot a nilTime ValidationsTime MethodsAnother ValidationOther minor changesIntelligence in the middlewareSourcesRailsProgramming today is a race between software Programming today is a race between software engineers striving to build better and bigger engineers striving to build better and bigger idiot-proof programs, and the Universe trying idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook No, I'm the Universe is winning. - Rick Cook No, I'm not Rick not RickInventory ApplicationSo, I was reading my emailTuesday morningA fellow that reports to me came in to talk about how we order and receive pcs and printersAnd I've been wanting to convert our machine move form to a web pageSuddenly, a real project…RequirementsA form that can handle both acquisitions and movesAn email hook so we can create remedy tickets as part of the processA mechanism for marking records as processed, and for moving data into our main inventory databaseThis should be simple, following KISSWhat we'll coverMigrations and more about Sqlite3ValidationsThe usualCreate a rails frameworkNote backslashesAlso, use of underscoresSome of this is bad thinking…rails inventoryrails inventorycd inventorycd inventoryruby script/generate scaffold Machine \ruby script/generate scaffold Machine \ user_name:string \user_name:string \ date_submitted:datetime \date_submitted:datetime \ ticket_number:integer \ticket_number:integer \ from_location:string \from_location:string \ to_location:string \to_location:string \ from_entity:string \from_entity:string \ to_entity:string \to_entity:string \ old_machine_name:string \old_machine_name:string \ new_machine_name:string \new_machine_name:string \ serial_number:string \serial_number:string \ unc_number:string \unc_number:string \ comments:text \comments:text \ status:string \status:string \ exported_to_main:boolean \exported_to_main:boolean \ unneeded_field:decimalunneeded_field:decimalGeneration of the first MigrationThe generation of the scaffold creates:The viewA controllerA modelAlso a database migration file in the db directory, in this case:20081104182035_create_machines.rb Note the timestamp and the conventional nameWhat does this file do?This file is a script, that contains a class, with two defined methodsOne methodcreates the database tablecreates initial fieldssets typesThe other methodundoes everything the first one doesclass CreateMachines < ActiveRecord::Migrationclass CreateMachines < ActiveRecord::Migration def self.updef self.up create_table :machines do |t|create_table :machines do |t| t.string :user_namet.string :user_name t.datetime :date_submittedt.datetime :date_submitted t.integer :ticket_numbert.integer :ticket_number t.string :from_locationt.string :from_location t.string :to_locationt.string :to_location t.string :from_entityt.string :from_entity t.string :to_entityt.string :to_entity t.string :old_machine_namet.string :old_machine_name t.string :new_machine_namet.string :new_machine_name t.string :serial_numbert.string :serial_number t.string :unc_numbert.string :unc_number t.text :commentst.text :comments t.string :statust.string :status t.boolean :exported_to_maint.boolean :exported_to_main t.decimal :unneeded_fieldt.decimal :unneeded_field t.timestampst.timestamps endend endend1st PartClass inherits from ActiveRecord::Migration self.up is a method applied when a migration is runA loop assigns type and namesdef self.downdef self.down drop_table :machinesdrop_table :machines endendendend2nd Parta second method provides a way to roll back the migrationDone properly, this allows one to move forward and back in database "versions" without affecting other structuresMigrationsYou can modify this file before applying it, adding additional options such as field lengths, default values, etcWhat's the point?Migrations allow manipulation of the database with some version controlYou could also manually manipulate the database, but you'd have to keep track of the changesBut some migrations are irreversible, and if you don't define a good way back….To protect against that, backup! Or use version control systems like cvs, subversion, gitschema.rbStored in db/This is the canonical representation of the current state of the databaseYou could modify this--don'tGenerated after each migrationYou can use this with db:schema:load to implement the same db structures on another systemActiveRecord::Schema.define :version => 20081105005808 doActiveRecord::Schema.define :version => 20081105005808 do create_table "machines", :force => true do |t|create_table "machines", :force => true do |t| t.string "user_name"t.string "user_name" t.datetime "date_submitted"t.datetime "date_submitted" t.integer "ticket_number"t.integer "ticket_number" t.string "from_location"t.string "from_location" t.string "to_location"t.string "to_location" t.string "from_entity"t.string "from_entity" t.string "to_entity"t.string "to_entity" t.string "old_machine_name"t.string "old_machine_name" t.string "new_machine_name"t.string "new_machine_name" t.string "serial_number"t.string "serial_number" t.integer "unc_number", :limit => 255t.integer "unc_number", :limit => 255 t.text "comments"t.text "comments" t.string "status"t.string "status" t.boolean "exported_to_main"t.boolean "exported_to_main" t.datetime "created_at"t.datetime "created_at" t.datetime
View Full Document