ActiveRecord,通过多态属性查找

fgu*_*len 6 ruby activerecord ruby-on-rails-3 active-record-query

有这个:

class Event < ActiveRecord::Base
  belongs_to :historizable, :polymorphic => true
end

user = User.create!
Run Code Online (Sandbox Code Playgroud)

我可以:

Event.create!(:historizable => user)
Run Code Online (Sandbox Code Playgroud)

但我不能:

Event.where(:historizable => user)
# Mysql2::Error: Unknown column 'events.historizable' in 'where clause'
Run Code Online (Sandbox Code Playgroud)

我必须这样做:

Event.where(:historizable_id => user.id, :historizable_type => user.class.name)
Run Code Online (Sandbox Code Playgroud)

更新

重现问题的代码:https://gist.github.com/fguillen/4732177#file-polymorphic_where_test-rb

fgu*_*len 3

\n

这已在 Rails master 中实现,并将在 Rails 4 中提供。谢谢。

\n
\n\n

\xe2\x80\x93 @carlosantoniodasilva

\n