Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert will_paginate and sorting...

Replytotopic

will_paginate and sorting

Posted in Forums : Ask a Rails expert

 
Profile

Authority 12
Posting Rating 0
Sign in to rate this post

I’ve implemented will_paginate with no problems. But I want to sort on by company_name, the user table is related to the companies table by company_id

user
belongs_to :company

company
has_many :users

sort_init ‘company_id’
sort_update
@users = User.paginate(:page => params[:page], :per_page => 10, :order => sort_clause, :include => :company)

Any ideas, unfortunately I get an error because the sql generated says order_by company_name and the sql generated is

Invalid column name ‘company_name’. HRESULT error code:0×80020009 Exception occurred.: SELECT * FROM (SELECT TOP 10 * FROM (SELECT TOP 10 users.[id] AS t0_r0, users.[employee_id] AS t0_r1, users.[user_name] AS t0_r2, users.[certificate] AS t0_r3, users.[date_valid_from] AS t0_r4, users.[date_valid_to] AS t0_r5, users.[status] AS t0_r6, users.[hashed_password] AS t0_r7, users.[email_address] AS t0_r8, users.[business_stream_id] AS t0_r9, users.[company_id] AS t0_r10, users.[time_zone] AS t0_r11, users.[language_id] AS t0_r12, users.[created_at] AS t0_r13, users.[created_by] AS t0_r14, users.[updated_at] AS t0_r15, users.[updated_by] AS t0_r16, users.[role_id] AS t0_r17, users.[salt] AS t0_r18, users.[enabled] AS t0_r19, companies.[id] AS t1_r0, companies.[company_registered_number] AS t1_r1, companies.[address_line_1] AS t1_r2, companies.[address_line_2] AS t1_r3, companies.[city] AS t1_r4, companies.[state] AS t1_r5, companies.[country_id] AS t1_r6, companies.[parent] AS t1_r7, companies.[licence] AS t1_r8, companies.[sales_tax_number] AS t1_r9, companies.[max_range_limits] AS t1_r10, companies.[created_on] AS t1_r11, companies.[created_at] AS t1_r12, companies.[updated_at] AS t1_r13, companies.[updated_on] AS t1_r14, companies.[created_by] AS t1_r15, companies.[updated_by] AS t1_r16, companies.[ultimate_parent] AS t1_r17, companies.[date_valid_from] AS t1_r18, companies.[date_valid_to] AS t1_r19, companies.[status] AS t1_r20, companies.[max_number_replies] AS t1_r21, companies.[company_name] AS t1_r22, companies.[domain_name] AS t1_r23 FROM users LEFT OUTER JOIN companies ON companies.id = users.company_id ORDER BY company_name asc) AS tmp1 ORDER BY company_name DESC) AS tmp2 ORDER BY company_name asc

ActiveRecord::StatementInvalid (DBI::DatabaseError: Execute OLE error code:80040E14 in Microsoft OLE DB Provider for SQL Server Invalid column name ‘company_name’.

 
Me

Authority 37
Posting Rating 100
Sign in to rate this post

Maybe try @users = User.paginate(:page => params[:page], :per_page => 10, :include => :company, :order => ‘companies.name DESC’).
If that doesn’t work, you can always implement your search in Ruby:

@users = User.paginate(:page => params[:page], :per_page => 10, :include => :company)
@users.sort! { |u1, u2| u1.company.name <=> u2.company.name }

HTH

 
20064666954644d813e6326

Authority 0
Posting Rating 74
Sign in to rate this post

As Clemens said.. use the table name in the sort order, especially if your not sorting on the main find model.

And Clemens other suggestion will not work. As @users is just going to be the first 10 results. His solution will sort by company_name but only in those first 10, so you won’t get the desired results.

 
Me

Authority 37
Posting Rating 100
Sign in to rate this post

Good catch, Tim, I didn’t think of that … (Note to self: Test code before putting it online!)

 
Profile

Authority 12
Posting Rating 0
Sign in to rate this post

Thanks a million for your time & efforts

Replytotopic

Other Recent Topics

Ask a Rails expert : How to use mephisto

Ask a Rails expert : How to use mephisto

Ask a Rails expert : will_paginate customization problem

Ask a Rails expert : BackgroundRB still wants 'development' environment...?

Ask a Rails expert : activescaffold, sql exception

Ask a Rails expert : Passing non-english chars in query string

Ask a Rails expert : Rails and 2D barcodes

Ask a Rails expert : apache giving proxy error

Ask a Rails expert : Custom Responses w/ 'extra' information...?

Ask a Rails expert : Log rotation in rails

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel