Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord, find by polymorphic attribute

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

like image 489
fguillen Avatar asked Feb 07 '13 14:02

fguillen


1 Answers

This has been implemented in Rails master and will be available in Rails 4. Thanks.

– @carlosantoniodasilva

like image 184
fguillen Avatar answered Oct 20 '22 08:10

fguillen