Problem when merging scopes and ordering via associations
First of all, I would like to express my love for meta_where. I switched it off for debugging this issue and realized how more elegant is the syntax and the power it adds to AR.
Enhanced order clauses break when merged from associations
class ToyBrand < ActiveRecord::Base
belongs_to :manu
has_many :toys
scope :meta_order, joins(:manu).order({:manu => :name}, :name) #works if called directly, not when merged
scope :join_order, joins(:manu).order('manus.name, toy_brands.name') #this does direct and merged
end
ToyBrand.meta_order #works
class Toy < ActiveRecord::Base
belongs_to :toy_brand
scope :order_via_meta, joins(:toy_brand).merge(ToyBrand.meta_order) # fails
scope :order_via_string, joins(:toy_brand).merge(ToyBrand.join_order) # works
end
Toy.order_via_meta #fails
Related issue (due to AR)
See https://github.com/rails/rails/issues/3002 With MetWhere active, the Toy.join_merge works!
In my test application everything works for this case. However, in my real application which is much more complicated, when I use the following scope:
class ShippingItem
belongs_to :item_variant
scope :ordering_test, joins(:item_variant).merge(ItemVariant.scoped)
end
like this:
Shipment.find(106).shipping_items.joins(:item_variant).ordering_test
I get:
MetaWhere::JoinDependency::ConfigurationError: Association named 'item_variants' was not found; perhaps you misspelled it?
.../meta_where-1.0.4/lib/meta_where/join_dependency.rb:26:in build_with_metawhere'When I call the same thing over the association like this, it works beautifully:Shipment.find(106).shipping_items.joins(:item_variant).merge(ItemVariant.scoped)`
I'm really puzzled by this. I tried every combination. I would like to encapsulate all sortings in the respective models so the controllers just call the scope containing the wanted sorting without seeing the implementation....
Thanks for the help, gam
Rails 3.0.9 / meta_where 1.0.4 / Ruby 1.9.2
I've just updated an app to Rails 3.1 and realized about that too. The point is that we were using meta_where before and switched to squeel and then the "Association not found" exception started to appear. It's funny because I never thought this "behavior" was expected to happen without meta_where and that indeed meta_where properly handles merging scopes with joins.
Can we get the "Enhanced relation merges" feature into Squeel?