Skip to content

Instantly share code, notes, and snippets.

@sicktastic
Last active February 3, 2018 21:01
Show Gist options
  • Save sicktastic/29cbdc55aa82ec9c364d6e3b2242e593 to your computer and use it in GitHub Desktop.
Save sicktastic/29cbdc55aa82ec9c364d6e3b2242e593 to your computer and use it in GitHub Desktop.
Clean, Organized, Mirror
create_table "students", id: :serial, force: :cascade do |t|
t.string "birth_year", limit: 255, null: false
t.string "country", limit: 255, null: false
t.string "first_name", limit: 255, null: false
t.string "last_name", limit: 255, null: false
t.string "student_status", limit: 255
t.text "story", null: false
t.integer "organization_id"
t.index ["organization_id"], name: "index_students_on_organization_id"
end
class Student < ApplicationRecord
belongs_to :organization
has_many :payments
has_many :student_timelines
has_many :subscriptions
has_many :users, through: :subscriptions
validates :birth_year, :first_name, :last_name, :country, length: {maximum: 255}, presence: true
validates :story, presence: true
end
require "rails_helper"
describe Student do
context "associations" do
it { should belong_to(:organization) }
it { should have_many(:payments) }
it { should have_many(:student_timelines) }
it { should have_many(:subscriptions) }
it { should have_many(:users).through(:subscriptions) }
end
context "validations" do
it { should validate_presence_of(:birth_year) }
it { should validate_presence_of(:country) }
it { should validate_presence_of(:first_name) }
it { should validate_presence_of(:last_name) }
it { should validate_presence_of(:story) }
it { should validate_length_of(:birth_year).is_at_most(255) }
it { should validate_length_of(:country).is_at_most(255) }
it { should validate_length_of(:first_name).is_at_most(255) }
it { should validate_length_of(:last_name).is_at_most(255) }
end
end
@sicktastic
Copy link
Author

  1. Clean spaces to distinguish different aspects of code
  2. Alphabetically ordered
  3. Model validation reflects database constraints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment