Advertisement
Guest User

Untitled

a guest
Jan 20th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. require 'pg'
  2. require 'active_record'
  3.  
  4. # Output messages from Active Record to standard out
  5. ActiveRecord::Base.logger = Logger.new(STDOUT)
  6.  
  7. puts 'Establishing connection to database ...'
  8. ActiveRecord::Base.establish_connection(
  9. adapter: 'postgresql',
  10. database: 'contactlist',
  11. username: 'development',
  12. password: 'development',
  13. host: 'localhost',
  14. port: 5432,
  15. pool: 5,
  16. encoding: 'unicode',
  17. min_messages: 'error'
  18. )
  19. puts 'CONNECTED'
  20. # Represents a person in an address book.
  21. class Contact < ActiveRecord::Base
  22.  
  23. class << self
  24. def update
  25. puts ('What contact do you want to update? Enter the id number')
  26. item = STDIN.gets.chomp
  27. puts ('What is the new name?')
  28. new_name = STDIN.gets.chomp
  29. puts ('What is the new email?')
  30. new_email = STDIN.gets.chomp
  31. @contact = Contact.find(item)
  32. @contact.name = new_name
  33. @contact.email = new_email
  34. @contact.save
  35. end
  36.  
  37. #Returns an Array of Contacts loaded from the database.
  38. def list_all
  39. record = Contact.all
  40. record.each do |item|
  41. puts "id is #{item.id}, name is #{item.name}, email is #{item.email}"
  42. end
  43. end
  44.  
  45. # Creates a new contact, adding it to the database, returning the new contact.
  46. def create(name, email)
  47. record = Contact.new
  48. record.name = name
  49. record.email = email
  50. record.save
  51. end
  52.  
  53. # Returns the contact with the specified id. If no contact has the id, returns nil.
  54. def finds(id)
  55. record = Contact.find(id)
  56. puts "id is #{record.id}, name is #{record.name}, email is #{record.email}"
  57. end
  58.  
  59. # Returns an array of contacts who match the given term.
  60. def searcher(term)
  61. # TODO: Select the Contact instances from the 'contacts.csv' file whose name or email attributes contain the search term.
  62. record =Contact.where("name LIKE ? OR email LIKE ?", "%#{term}%", "%#{term}%")
  63. record.each do |item|
  64. puts "id is #{item.id}, name is #{item.name}, email is #{item.email}"
  65. end
  66. end
  67.  
  68. end
  69.  
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement