Skip to content

Instantly share code, notes, and snippets.

@reidiculous
Created March 4, 2011 15:21
Show Gist options
  • Save reidiculous/854748 to your computer and use it in GitHub Desktop.
Save reidiculous/854748 to your computer and use it in GitHub Desktop.
activerecord touch test
require 'sqlite3'
require 'activerecord'
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => "touch.sqlite3")
ActiveRecord::Base.connection.execute <<-SQL
create table companies(
id integer primary key asc,
created_at text,
updated_at text
);
SQL
ActiveRecord::Base.connection.execute <<-SQL
create table departments(
id integer primary key asc,
company_id integer,
created_at text,
updated_at text
);
SQL
ActiveRecord::Base.connection.execute <<-SQL
create table employees(
id integer primary key asc,
department_id integer,
created_at text,
updated_at text
);
SQL
class Company < ActiveRecord::Base
has_many :departments
end
class Department < ActiveRecord::Base
belongs_to :company, :touch => true
has_many :employees
end
class Employee < ActiveRecord::Base
belongs_to :department, :touch => true
end
c = Company.create!
d = c.departments.create!
e = c.departments.first.employees.create!
sleep 1
e.touch
p e.updated_at
p d.updated_at
p c.updated_at
if d.updated_at.sec != e.updated_at.sec
puts 'department not updated!'
end
if c.updated_at.sec != e.updated_at.sec
puts 'company not updated!'
end
system "rm touch.sqlite3"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment