Skip to content

Instantly share code, notes, and snippets.

@mikeraimondi
mikeraimondi / boolean_default_example.rb
Created June 5, 2013 03:12
boolean default example
class CreateWidgets < ActiveRecord::Migration
def change
create_table :widgets do |t|
t.string :name, null: false
t.string :description
t.boolean :flanged, default: false
t.timestamps
end
end
@mikeraimondi
mikeraimondi / validation_example_1.rb
Created June 9, 2013 15:30
association validation example
class CrazyCatLady < ActiveRecord::Base
has_many :cats,
inverse_of: :crazy_cat_lady
end
class Cat < ActiveRecord::Base
belongs_to :crazy_cat_lady,
inverse_of: :cats
validates_presence_of :crazy_cat_lady
class CreateCats < ActiveRecord::Migration
def change
create_table :cats do |t|
t.string :color
t.integer :crazy_cat_lady_id, null: false
t.timestamps
end
end
end
@mikeraimondi
mikeraimondi / user.rb
Created June 15, 2013 03:54
expressive associations example
class User < ActiveRecord::Base
has_many :posts,
inverse_of: :user
attr_accessible :name, :email
end
@mikeraimondi
mikeraimondi / post.rb
Created June 15, 2013 03:55
expressive association example
class Post < ActiveRecord::Base
belongs_to :user,
inverse_of: :posts
attr_accessible :body, :title
end
@mikeraimondi
mikeraimondi / user.rb
Created June 15, 2013 04:10
expressive association example
class User < ActiveRecord::Base
has_many :posts,
foreign_key: 'author_id',
inverse_of: :author
attr_accessible :name, :email
end
@mikeraimondi
mikeraimondi / post.rb
Created June 15, 2013 04:11
expressive association example
class Post < ActiveRecord::Base
belongs_to :author,
class_name: 'User',
foreign_key: 'author_id',
inverse_of: :posts
attr_accessible :body, :title
end
@mikeraimondi
mikeraimondi / database_cleaner.rb
Created June 29, 2013 00:25
database_cleaner config for Capybara/RSpec with poltergeist
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = Capybara.current_driver == :rack_test ? :transaction : :truncation
DatabaseCleaner.clean
DatabaseCleaner.start
end
@mikeraimondi
mikeraimondi / lessons.rb
Created July 9, 2013 02:13
Seeder for Memworks lessons
module Seeders
module Lessons
class << self
def seed
Lesson.destroy_all
lessons = []
base_path = 'db/seed_data'
Dir.glob(Rails.root.join(base_path, 'lessons', '*.yml')) do |file|
lesson = YAML.load_file(file)
class IntervalWorker
def initialize(interval_id)
@interval_id = interval_id
end
def perform
interval = Interval.where(id: @interval_id).first
interval.complete! if interval.present?
end
end