Skip to content

Instantly share code, notes, and snippets.

@mvastola
Created November 29, 2012 12:40
Show Gist options
  • Save mvastola/4168795 to your computer and use it in GitHub Desktop.
Save mvastola/4168795 to your computer and use it in GitHub Desktop.
ActiveRecord validates_associated Issue in Rails 3.2.9
#!/usr/bin/env ruby1.9.1
require 'rubygems'
gem 'rails', '3.2.9'
gem 'sqlite3'
require "active_record/railtie"
require "active_resource/railtie"
ActiveRecord::Base.logger = Logger.new STDOUT
module Vatest
class Application < Rails::Application
end
end
ENV['DATABASE_URL'] = 'sqlite3://localhost/db.sqlite3'
Vatest::Application.configure do
config.cache_classes = false
config.whiny_nils = true
config.active_support.deprecation = :log
config.class.send :define_method, :database_configuration do
nil
end
end
Vatest::Application.initialize!
class Author < ActiveRecord::Base
has_many :books
validates :name, :presence => true, :uniqueness => true
end
class Book < ActiveRecord::Base
belongs_to :author
validates :title, :presence => true, :uniqueness => { :scope => :author_id }
validates :author_id, :presence => true
validates_associated :author
end
class CreateAuthors < ActiveRecord::Migration
def change
create_table :authors do |t|
t.string :name, :null => false
end
add_index :authors, :name, :unique => true
end
end
class CreateBooks < ActiveRecord::Migration
def change
create_table :books do |t|
t.references :author, :null => false
t.string :title, :null => false
end
add_index :books, [ :author_id, :title ], :unique => true
end
end
to_eval = [
'CreateAuthors.migrate(:up) if !Author.table_exists?',
'CreateBooks.migrate(:up) if !Book.table_exists?',
'Author.destroy_all',
'Book.destroy_all',
'$b = Book.new(:title => "Testing 1.2.3...", :author_id => Math::PI * 1000)',
'$b.valid?',
'$b.save',
'$b.reload',
'$b.valid?'
]
to_eval.each do |rb|
print "# " + rb + "\n"
print "==> " + eval(rb).inspect + "\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment