I have the following definition class A < ActiveRecord::Base has_many bs, :order => ''fecha ASC, id ASC'' end somewhere in my app, I need to sort them differently: some_a = A.find(something) some_b = a.bs.find(:first, :conditions => [''fecha <= ?'', some_date], :order => ''fecha DESC, id DESC'') this is creating the following SQL: SELECT * FROM bs WHERE (bs.a_id = 1 AND (fecha <= ''2007-02-01'')) ORDER BY fecha DESC, id DESC, fecha ASC, id ASC LIMIT 1 wich is really weird... How can I replace the order of the relation only for that query? Looking at the rails code[1], it seems that the only (simple) way to do that would be to manually do the relation, something like this: some_b = B.find(:first, :conditions => [''a_id = ? AND fecha <= ?'', some_a.id, some_date], :order => ''fecha DESC, id DESC) [1] active_record(1.15.1)/associations/has_many.rb, line 81 if options[:order] && @reflection.options[:order] options[:order] = "#{options[:order]}, #{@reflection.options[:order]}" elsif @reflection.options[:order] options[:order] = @reflection.options[:order] end line 82 seems to be where the :orders are mixed. Which is fine if they''re not the same columns... any idea? regards, Rolando.- -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 2/1/07, Rolando Abarca <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I have the following definition > > class A < ActiveRecord::Base > has_many bs, :order => ''fecha ASC, id ASC'' > end > > somewhere in my app, I need to sort them differently: > > some_a = A.find(something) > some_b = a.bs.find(:first, > :conditions => [''fecha <= ?'', some_date], > :order => ''fecha DESC, id DESC'') > > this is creating the following SQL: > > SELECT * FROM bs WHERE (bs.a_id = 1 AND (fecha <= ''2007-02-01'')) ORDER > BY fecha DESC, id DESC, fecha ASC, id ASC LIMIT 1 > > wich is really weird... How can I replace the order of the relation only > for that query? Looking at the rails code[1], it seems that the only > (simple) way to do that would be to manually do the relation, something > like this: > > some_b = B.find(:first, > :conditions => [''a_id = ? AND fecha <= ?'', > some_a.id, > some_date], > :order => ''fecha DESC, id DESC) > > [1] active_record(1.15.1)/associations/has_many.rb, line 81 > if options[:order] && @reflection.options[:order] > options[:order] = "#{options[:order]}, > #{@reflection.options[:order]}" > elsif @reflection.options[:order] > options[:order] = @reflection.options[:order] > end > > line 82 seems to be where the :orders are mixed. Which is fine if > they''re not the same columns... > > any idea? > regards, > Rolando.-Jamis just blogged about this recently: http://weblog.jamisbuck.org/2007/1/18/activerecord-association-scoping-pitfalls Scroll down towards the bottom to see examples of both solving the problem using a second extension and using association extensions. Hope that helps. -- Zack Chandler http://depixelate.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Zack Chandler wrote:> Jamis just blogged about this recently: > > http://weblog.jamisbuck.org/2007/1/18/activerecord-association-scoping-pitfallsnice!!! thanks a lot :-D> Scroll down towards the bottom to see examples of both solving the > problem using a second extension and using association extensions. > > Hope that helps. > > -- > Zack Chandler > http://depixelate.comRegards, Rolando.- -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---