Skip to content

Instantly share code, notes, and snippets.

@fguillen
Last active December 12, 2015 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fguillen/4732177 to your computer and use it in GitHub Desktop.
Save fguillen/4732177 to your computer and use it in GitHub Desktop.
Code example to reproduce the issue "ActiveRecord, find by polymorphic attribute" http://stackoverflow.com/q/14753611/316700
.rvmrc
polymorphic.sqlite
rvm use --create ruby-1.9.3-p286@polymorphic_where
rvm use --create ruby-1.9.3_p286@polymorphic_where"
source :rubygems
gem "activerecord"
gem "sqlite3"
GEM
remote: http://rubygems.org/
specs:
activemodel (3.2.11)
activesupport (= 3.2.11)
builder (~> 3.0.0)
activerecord (3.2.11)
activemodel (= 3.2.11)
activesupport (= 3.2.11)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activesupport (3.2.11)
i18n (~> 0.6)
multi_json (~> 1.0)
arel (3.0.2)
builder (3.0.4)
i18n (0.6.1)
multi_json (1.5.0)
sqlite3 (1.3.7)
tzinfo (0.3.35)
PLATFORMS
ruby
DEPENDENCIES
activerecord
sqlite3
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment