ActiveRecord 4 has some nice features for handling polymorphic associations that fail when squeel is added. For example, you can do this:
belongs_to :item, :polymorphic => true
item = Item.first
post = Post.where(:item => item).first
This works in vanilla ActiveRecord but fails in squeel, apprently because it is treating the activerecord object as a symbol.
I'm hitting something similar, only in an even simpler case: it's not polymorphic. I believe if :item is not polymorphic in the above example, it will work in AR4 and not in Squeel.
AR4 seems to recognize :association_name => record in a where and turn it into :association_foreign_key => record.id (or, if it's polymorphic, :association_foreign_key => record.id, :association_foreign_type => record.class.name). Squeel appears to break that.
:association_name => record
:association_foreign_key => record.id
:association_foreign_key => record.id, :association_foreign_type => record.class.name
In fact, here's the documentation: https://github.com/rails/rails/blob/1e583f81d209ef097e040a6a7f2145ab717153b6/activerecord/lib/active_record/relation/query_methods.rb#L502
Found it. AR's implementation of #build_where uses PredicateBuilder:
Which expands associations:
But Squeel's implementation skips this part (apparently intentionally?):
Can we hope for a fix?
I'm just acknowledging that I've seen this issue, since I have gotten some e-mail about it.
Long story short, I was in Kenya a couple of weeks ago with no computer, and I'm strapped for time now that I've returned. If someone wants to put in a PR, I'm happy to look it over and merge if it checks out, but in the meantime, I'll just ask for your patience.
Also, @Peeja, just to clarify, the behavior should be identical to AR where possible, so this is definitely not intentional. The reason we skip PredicateBuilder is because we lazily convert Squeel nodes, etc into their Arel counterparts at query time, instead of when creating the where.
Thanks for the solution. I'm unable to test it because I rewrote some parts to use pure AR instead of squeel, but glad to see a fix 👍