Skip to content

Instantly share code, notes, and snippets.

@assembler
Created October 12, 2011 15:19
Show Gist options
  • Save assembler/1281490 to your computer and use it in GitHub Desktop.
Save assembler/1281490 to your computer and use it in GitHub Desktop.
validates_associated ignores :if option
# see: http://jonathanleighton.com/articles/2011/awesome-active-record-bug-reports/
require 'test/unit'
require 'ruby-debug'
require 'active_record'
puts "Active Record #{ActiveRecord::VERSION::STRING}"
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => ':memory:'
)
ActiveRecord::Schema.define do
create_table :products, :force => true do |t|
t.string :name
t.integer :category_id
t.timestamps
end
create_table :categories, :force => true do |t|
t.string :name
t.timestamps
end
end
class Product < ActiveRecord::Base
belongs_to :category
validates :name, :presence => true
end
class Category < ActiveRecord::Base
has_many :products
validates :name, :presence => true
validates_associated :products, :if => :false_method
def false_method
false
end
end
class BoomTest < Test::Unit::TestCase
def test_save_ok
c = Category.new :name => "Foo"
c.products << Product.new
assert c.save!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment