Last active
December 12, 2015 06:49
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.rvmrc | |
polymorphic.sqlite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rvm use --create ruby-1.9.3-p286@polymorphic_where |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rvm use --create ruby-1.9.3_p286@polymorphic_where" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source :rubygems | |
gem "activerecord" | |
gem "sqlite3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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