【发布时间】:2013-01-23 02:23:44
【问题描述】:
有这个:
class Event < ActiveRecord::Base
belongs_to :historizable, :polymorphic => true
end
user = User.create!
我可以:
Event.create!(:historizable => user)
但我不能:
Event.where(:historizable => user)
# Mysql2::Error: Unknown column 'events.historizable' in 'where clause'
我必须这样做:
Event.where(:historizable_id => user.id, :historizable_type => user.class.name)
更新
重现问题的代码:https://gist.github.com/fguillen/4732177#file-polymorphic_where_test-rb
【问题讨论】:
-
我知道如果我正确配置了
has_many,我可以做到user.events,但我正在寻找where的可能性。 -
刚刚在rails项目中打开了一个bug,看看他们的想法:github.com/rails/rails/issues/9227
标签: ruby ruby-on-rails-3 activerecord active-record-query