Skip to content

Instantly share code, notes, and snippets.

@quake
Created January 14, 2009 06:32
Show Gist options
  • Save quake/46815 to your computer and use it in GitHub Desktop.
Save quake/46815 to your computer and use it in GitHub Desktop.
#schema.rb
ActiveRecord::Schema.define(:version => 2) do
create_table "stories", :force => true do |t|
t.string "title", "subtitle"
t.string "type"
t.integer "length_counter", :default => 0
t.boolean "published"
end
create_table "characters", :force => true do |t|
t.integer "story_id"
t.string "name"
end
end
#spec_helper.rb
Character.class_eval do
index [:name, :story_id]
index [:id, :story_id]
index [:id, :name, :story_id]
belongs_to :story
def after_create
story.increment(:length_counter, self.name.length) if self.name && story
end
end
#spec code
it 'increments the count in callback' do
story = Story.create!(:title => 'story')
story.length_counter.should == 0
character = story.characters.create!(:name => "character")
story.length_counter.should == character.name.length
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment