Advertisement
solidsnake

12/10

Dec 9th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 1.48 KB | None | 0 0
  1. Model review:
  2.  
  3. 1) rails new addressbook
  4.     -cd into addressbook
  5. 2) edit the routes.rb
  6.  
  7. root(:to => "pages#index")
  8. root :to => "pages#index"
  9.  
  10. 3) Create corresponding controller#method
  11.  
  12. File: pages_controller.rb
  13. implementation:
  14.  
  15. class PagesController < ApplicationController
  16.     def index
  17.         render :template => "pages/index"
  18.     end
  19. end
  20.  
  21. file: app/views/pages/index.html.erb
  22.  
  23. table name: contacts
  24. attributes:
  25.     first_name
  26.     last_name
  27.     email
  28.     contact_number
  29.  
  30. #create the migration and model files
  31. command: rails generate model Contact
  32. last_name:string email:string
  33. contact_number:string address:text
  34.  
  35.  
  36. bundle install
  37.  
  38. #migrate the migration command into db
  39. rake db: migrate
  40.  
  41. #check db/schema.rb
  42.  
  43. 4. add more columns into database
  44. do not edit db/schema.rb
  45.  
  46. rails generate migration add_middle_name_to_contacts
  47.  
  48. #in the migratuon under def change
  49.     add_column(:table_name, :field_name, :data_type)
  50.     add_column(:contacts, :middle_name, :string)
  51.  
  52. 4.1 add fixture or set of data into our database
  53.  
  54. contact = Contact.new
  55. contact.first_name = "Raphael"
  56. contact.middle_name = "B"
  57. contact.last_name = "Alampay"
  58. contact.email = :raphael.alampay@gmail.com"
  59. contact.contact_number = "741412"
  60.  
  61. contact.save
  62.  
  63. 4.2 perform a SELECT statement for only 1 record based on id
  64.  
  65. id - 1
  66. c - Contact.find(id)
  67.  
  68. 4.3 perform SELECT all statemetn that returns an arraty of objects
  69.  
  70. contacts = Contact.all
  71. contacts.each do |c|
  72. puts c.full_name
  73. end
  74.  
  75. 5 display all contacts in root page
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement