Adding Data to a Database Table - PowerPoint PPT Presentation

About This Presentation
Title:

Adding Data to a Database Table

Description:

... [ {:isbn = '4567-8901', :author = 'Chekov', :title = 'The Cherry Orchard'}, {:isbn = '3412-4598', :author = 'Hemmingway', : title ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 7
Provided by: Cwo52
Learn more at: http://csis.pace.edu
Category:

less

Transcript and Presenter's Notes

Title: Adding Data to a Database Table


1
Adding Data to a Database Table
  • Carol Wolf
  • Computer Science

2
Adding data to a books table
  • Create a books table using
  • rails generate scaffold book isbnstring
    authorstring titlestring
  • We can add data to the table using the New Book
    link at the bottom of the index page.
  • This is slow and for tables with a large number
    of fields very cumbersome.
  • Instead a lot of data can be added at once using
    either a migration or modifying seeds.rb in the
    db folder.

3
Using a migration
  • Create a migration using
  • rails generate migration add_data
  • This produces
  • class AddData lt ActiveRecordMigration
  • def self.up
  • end
  •  
  • def self.down
  • end
  • end
  • Add data to get the following

4
  • class AddData lt ActiveRecordMigration
  • def self.up
  • Book.create(
  • isbn gt '4567-8901',
  • author gt 'Chekov',
  • title gt 'The Cherry Orchard'
  • )
  • Book.create(
  • isbn gt 3412-4598,
  • author gt Hemmingway,
  • title gt The Sun Also Rises
  • )
  • end
  •  
  • def self.down
  • Book.delete_all
  • end
  • end

5
Modifying seeds.rb
  • seeds.rb is in the db folder.
  • It is generated along with everything else when a
    new rails project is created.
  • This file should contain all the record
    creation needed to seed the database with its
    default values.
  • The data can then be loaded with the rake
    dbseed (or created alongside the db with
    dbsetup).
  • Examples
  • cities City.create( name gt 'Chicago' ,
    name gt 'Copenhagen' )
  • Mayor.create(name gt 'Daley', city gt
    cities.first)
  • You can add data to it using the form indicated.
  • It is run using rake dbseed. (Note no plural)

6
  • This file should contain all the record
    creation needed to seed the database with its
    default values.
  • The data can then be loaded with the rake
    dbseed (or created alongside the db with
    dbsetup).
  • Examples
  • cities City.create( name gt 'Chicago' ,
    name gt 'Copenhagen' )
  • Mayor.create(name gt 'Daley', city gt
    cities.first)
  •  
  • books Book.create(
  • isbn gt '4567-8901', author gt 'Chekov',
    title gt 'The Cherry Orchard',
  • isbn gt '3412-4598', author gt
    'Hemmingway', title gt 'The Sun Also Rises',
  • isbn gt '1357-2468', author gt
    'Steinbeck', title gt 'The Grapes of Wrath'
  • )
  • Note the curly braces around the hashes and the
    square brackets around all the data.
  • This is shorter than the migration and easier to
    do. But they both work.
Write a Comment
User Comments (0)
About PowerShow.com