问题
Having this:
class Event < ActiveRecord::Base
belongs_to :historizable, :polymorphic => true
end
user = User.create!
I can:
Event.create!(:historizable => user)
But I can't:
Event.where(:historizable => user)
# Mysql2::Error: Unknown column 'events.historizable' in 'where clause'
I have to do this instead:
Event.where(:historizable_id => user.id, :historizable_type => user.class.name)
Update
Code that reproduces the issue: https://gist.github.com/fguillen/4732177#file-polymorphic_where_test-rb
回答1:
This has been implemented in Rails master and will be available in Rails 4. Thanks.
– @carlosantoniodasilva
回答2:
I do this:
user.events
This is a proper AR query, you can chain it with other scopes and stuff:
user.events.where(<your event conditions here>)
EDIT: AFAIK the other way around you must specify both fields (makes sense: you could have a user with id 4 and another thing with events, like a Party, also with id 4).
EDIT2: Regarding "Why does create
work and where
doesn't": create
is more highlevel than where
, because the former deals with "a complete model", while the latter manages things at the database table level.
- ActiveRecord's
create
(AFAIK) uses a combination ofnew
+update_param
somewhere down the line. update_param
uses your model'sxxxx=
methods for assigning each individual property.- In your example,
historizable=
is a method built by thebelongs_to
expression. Since thebelongs_to
"knows" that it's polymorphic, it can deal with the type and id.
On the other hand, when you pass a hash to the where
clause, the parameters there only refer to database fields. Rails provides scopes for "higher level" access:
class Event < ActiveRecord::Base
...
scope :by_historizable, lambda { |h| where(:historizable_id => h.id, :historizable_type => h.class.name) }
end
...
Event.by_historizable(user).where(<your other queries here>)
I've heard that this might change in Rails 4, and where
might be more "intelligent". But I have not checked yet.
回答3:
Try:
Event.joins(:historizable).where(:historizable => {:historizable_type => user})
来源:https://stackoverflow.com/questions/14753611/activerecord-find-by-polymorphic-attribute