Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Problem when merging scopes and ordering via associations #40

Open
gamov opened this issue Sep 13, 2011 · 2 comments
Open

Problem when merging scopes and ordering via associations #40

gamov opened this issue Sep 13, 2011 · 2 comments

Comments

@gamov
Copy link

gamov commented Sep 13, 2011

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 rails/rails#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

@knoopx
Copy link

knoopx commented Nov 17, 2011

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.

@knoopx
Copy link

knoopx commented Nov 17, 2011

Can we get the "Enhanced relation merges" feature into Squeel?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants