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