Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

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

View .gitignore
1
rvm use --create ruby-1.9.3-p286@polymorphic_where
View .gitignore
1
rvm use --create ruby-1.9.3_p286@polymorphic_where"
View .gitignore
1 2 3 4
source :rubygems
 
gem "activerecord"
gem "sqlite3"
View .gitignore
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
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
View .gitignore
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.