Created
April 24, 2013 23:27
-
-
Save alekseyg/5456450 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'active_record' | |
# Print out what version we're running | |
puts "Active Record #{ActiveRecord::VERSION::STRING}" | |
# Connect to an in-memory sqlite3 database | |
ActiveRecord::Base.establish_connection( | |
:adapter => 'sqlite3', | |
:database => ':memory:' | |
) | |
# Create the minimal database schema necessary to reproduce the bug | |
ActiveRecord::Schema.define do | |
create_table "photos", :force => true do |t| | |
t.integer "posting_id" | |
end | |
create_table "postings", :force => true do |t| | |
t.integer "photos_count", :default => 0 | |
end | |
end | |
# define the models | |
class Photo < ActiveRecord::Base | |
belongs_to :posting, :counter_cache => true | |
end | |
class Posting < ActiveRecord::Base | |
has_many :photos | |
accepts_nested_attributes_for :photos, :allow_destroy => true | |
end | |
### Here's the bugs: | |
posting = Posting.create(:photos_attributes => [Photo.new.attributes]) | |
posting.reload | |
puts "Create posting with one photo via nested form:" | |
puts "Posting\#photos_count: #{posting.photos_count}; Expected: 1" | |
puts "Posting\#photos.count: #{posting.photos.count}; Expected: 1" | |
puts "Posting\#photos.size: #{posting.photos.size}; Expected: 1" | |
posting.update_attributes(:photos_attributes => [:id => posting.photos.first.id, :_destroy => true]) | |
posting.reload | |
puts "Delete posting's photo via nested form:" | |
puts "Posting\#photos_count: #{posting.photos_count}; Expected: 0" | |
puts "Posting\#photos.count: #{posting.photos.count}; Expected: 0" | |
puts "Posting\#photos.size: #{posting.photos.size}; Expected: 0" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment