Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'pg'
- require 'active_record'
- # Output messages from Active Record to standard out
- ActiveRecord::Base.logger = Logger.new(STDOUT)
- puts 'Establishing connection to database ...'
- ActiveRecord::Base.establish_connection(
- adapter: 'postgresql',
- database: 'contactlist',
- username: 'development',
- password: 'development',
- host: 'localhost',
- port: 5432,
- pool: 5,
- encoding: 'unicode',
- min_messages: 'error'
- )
- puts 'CONNECTED'
- # Represents a person in an address book.
- class Contact < ActiveRecord::Base
- class << self
- def update
- puts ('What contact do you want to update? Enter the id number')
- item = STDIN.gets.chomp
- puts ('What is the new name?')
- new_name = STDIN.gets.chomp
- puts ('What is the new email?')
- new_email = STDIN.gets.chomp
- @contact = Contact.find(item)
- @contact.name = new_name
- @contact.email = new_email
- @contact.save
- end
- #Returns an Array of Contacts loaded from the database.
- def list_all
- record = Contact.all
- record.each do |item|
- puts "id is #{item.id}, name is #{item.name}, email is #{item.email}"
- end
- end
- # Creates a new contact, adding it to the database, returning the new contact.
- def create(name, email)
- record = Contact.new
- record.name = name
- record.email = email
- record.save
- end
- # Returns the contact with the specified id. If no contact has the id, returns nil.
- def finds(id)
- record = Contact.find(id)
- puts "id is #{record.id}, name is #{record.name}, email is #{record.email}"
- end
- # Returns an array of contacts who match the given term.
- def searcher(term)
- # TODO: Select the Contact instances from the 'contacts.csv' file whose name or email attributes contain the search term.
- record =Contact.where("name LIKE ? OR email LIKE ?", "%#{term}%", "%#{term}%")
- record.each do |item|
- puts "id is #{item.id}, name is #{item.name}, email is #{item.email}"
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement