Hi, I am stuck pretty bad please help. any comment is helpful. I have say three models Customer(fields are id, name, cell_phone, primary_address_id) belongs_to :primary_address, :class_name => ''CustomerAddress'', :foreign_key => ''primary_address_id'' has_many :customer_addresses end CustomerAddress(fields city, customer_id, state) belongs_to :customer end so basically I have many addresses for a customer, and one of which is primary address. so I have this relations ship customer.primary_address. Now I want to write a query by using find or paginate. like: Customer.find [arra of id], :order => "primary_address.city ASC" The problem is that it is not ordering by associated columns. It says no column primary_address.city found. I am trying to use include but it do not works, :join how can I use it. Please help is there any way to order by associated columns. Thanks Sumeet Panchal -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sumeet Panchal wrote:> Hi, > > I am stuck pretty bad please help. any comment is helpful. > I have say three models > Customer(fields are id, name, cell_phone, primary_address_id) > belongs_to :primary_address, :class_name => > ''CustomerAddress'', :foreign_key => ''primary_address_id'' > has_many :customer_addresses > end > > CustomerAddress(fields city, customer_id, state) > belongs_to :customer > end > > so basically I have many addresses for a customer, and one of which is > primary address. so I have this relations ship > customer.primary_address. > > Now I want to write a query by using find or paginate. like: > Customer.find [arra of id], :order => "primary_address.city ASC" > The problem is that it is not ordering by associated columns. It says > no column primary_address.city found. > I am trying to use include but it do not works, :join how can I use > it. Please help is there any way to order by associated columns. > > Thanks > Sumeet PanchalYou can use join as Customer.find(:all, :joins => "customers inner join customer_address as b on customers.primary_address_id=b.id") and in this you can use ordering. Anubhaw -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
join will work, even :include will work. but this reduce performance for query. On Apr 23, 12:01 pm, Anubhaw Prakash <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Sumeet Panchal wrote: > > Hi, > > > I am stuck pretty bad please help. any comment is helpful. > > I have say three models > > Customer(fields are id, name, cell_phone, primary_address_id) > > belongs_to :primary_address, :class_name => > > ''CustomerAddress'', :foreign_key => ''primary_address_id'' > > has_many :customer_addresses > > end > > > CustomerAddress(fields city, customer_id, state) > > belongs_to :customer > > end > > > so basically I have many addresses for a customer, and one of which is > > primary address. so I have this relations ship > > customer.primary_address. > > > Now I want to write a query by using find or paginate. like: > > Customer.find [arra of id], :order => "primary_address.city ASC" > > The problem is that it is notorderingbyassociatedcolumns. It says > > no column primary_address.city found. > > I am trying to use include but it do not works, :join how can I use > > it. Please help is there any way to order byassociatedcolumns. > > > Thanks > > Sumeet Panchal > > You can use join as Customer.find(:all, :joins => "customers inner join > customer_address as b on customers.primary_address_id=b.id") and in this > you can useordering. > > Anubhaw > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.