Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
source 'https://rubygems.org'

gem 'activerecord', :require => 'active_record'
gem 'faker'
12 changes: 12 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'rake'
require 'rspec/core/rake_task'
require 'faker'
require_relative 'app/models/teacher'
require_relative 'db/config'
require_relative 'lib/students_importer'

Expand Down Expand Up @@ -28,6 +30,16 @@ task "db:populate" do
StudentsImporter.import
end

task "db:populate_teacher" do
10.times do
teacher = Teacher.create! (
{:first_name => Faker::Name.first_name,
:last_name => Faker::Name.last_name,
:email => Faker::Internet.email,
:phone => Faker::PhoneNumber.phone_number })
end
end

desc 'Retrieves the current schema version number'
task "db:version" do
puts "Current version: #{ActiveRecord::Migrator.current_version}"
Expand Down
25 changes: 24 additions & 1 deletion app/models/student.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
require_relative '../../db/config'

# implement your Student model here
class Student < ActiveRecord::Base

validates :email, :uniqueness => true
validates :email, :format => { :with => /\S+[@]\w+[.]\w{2,}/, :message => "Invalid email" }
validates :age, :numericality => {:greater_than => 4}
validates :sanitized_number, :numericality => {:greater_than => 9}

# Does it matter if age and sanitized_number are instance variables (@age and @sanitized_number)?


def name
name = "#{first_name} #{last_name}"
end

def age
now.year - birthday.year - ((now.month > birthday.month || (now.month == birthday.month && now.day >= birthday.day)) ? 0 : 1)
end

def sanitized_number
phone.gsub(/\D/, '').length
end
end


7 changes: 7 additions & 0 deletions app/models/teachers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require_relative '../../db/config'

class Teacher < ActiveRecord::Base

validates :email, :uniqueness => true

end
13 changes: 11 additions & 2 deletions db/migrate/20121011144238_create_students.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
require_relative '../config'

# this is where you should use an ActiveRecord migration to

class CreateStudents < ActiveRecord::Migration
def change
# HINT: checkout ActiveRecord::Migration.create_table
create_table :students do |t|
t.integer :id
t.string :first_name
t.string :last_name
t.string :gender
t.string :email
t.string :phone
t.date :birthday
# t.datetime :created_at
# t.datetime :updated_at
end
end
end
17 changes: 17 additions & 0 deletions db/migrate/20121011144238_create_teachers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require_relative '../config'


class CreateStudents < ActiveRecord::Migration
def change
create_table :teachers do |t|
t.integer :id
t.string :first_name
t.string :last_name
t.string :gender
t.string :email
t.string :phone
# t.datetime :created_at
# t.datetime :updated_at
end
end
end
2 changes: 1 addition & 1 deletion spec/migrate_create_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}

ActiveRecord::Base.connection.columns(:students).each do |col|
expected[col.type].include?(col.name).should be_true
expected[col.type].include?(col.name).should be_true unless expected[col.type].nil?
end
end
end