public
Last active

Code example to reproduce the issue "ActiveRecord, find by polymorphic attribute" http://stackoverflow.com/q/14753611/316700

  • Download Gist
.gitignore
1 2
.rvmrc
polymorphic.sqlite
.rvmrc
1
rvm use --create ruby-1.9.3-p286@polymorphic_where
.rvmrc.example
1
rvm use --create ruby-1.9.3_p286@polymorphic_where"
Gemfile
Ruby
1 2 3 4
source :rubygems
 
gem "activerecord"
gem "sqlite3"
polymorphic_where_test.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
require "active_record"
require "minitest/unit"
require "minitest/autorun"
 
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => "#{File.dirname(__FILE__)}/polymorphic.sqlite"
)
 
ActiveRecord::Schema.define :version => 0 do
create_table :users, :force => true do |t|
end
 
create_table :events, :force => true do |t|
t.integer :historizable_id
t.string :historizable_type
end
end
 
class Event < ActiveRecord::Base
belongs_to :historizable, :polymorphic => true
end
 
class User < ActiveRecord::Base
has_many :events, :as => :historizable
end
 
class PolymorphicWhereTest < MiniTest::Unit::TestCase
def test_polymorphic_where
user = User.create!
event = Event.create!(:historizable => user)
 
assert_equal(event, Event.where(:historizable_id => user.id, :historizable_type => user.class.name).first)
assert_equal(event, Event.where(:historizable => user).first)
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.