PiRSquared17 / activescaffold

Automatically exported from code.google.com/p/activescaffold
MIT License
0 stars 0 forks source link

SQL StatementInvalid with nested scaffold when applying sort_by :sql on associated field #721

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Have the following model code:
def Company
  has_many: contacts
end

def Contact
 belongs_to :company
end

2. The following controller code
class CompaniesController   
 active_scaffold do |config|
   # adding action link to nested contacts scaffold
   config.nested.add_link("Contacts", [:contacts])
 end
end

class ContactsController    
 active_scaffold do |config|
  config.list.sorting = [{:company => :asc}, { :full_name => :asc }]
  # removing the next line fixes the error, but no longer sorted
  config.columns[:company].sort_by :sql => "companies.name"
  config.columns[:full_name].sort_by :sql => "contacts.full_name"
 end
end

3. The sorting and everything works fine when in Contacts controller list
action. But if I click the "Contacts" action link from within Companies
controller I get an error:
ActiveRecord::StatementInvalid
Mysql::Error: Unknown column 'companies.name' in 'order clause': SELECT
`contacts`.* FROM `contacts`  WHERE (((contacts.company_id = '212'))) 
ORDER BY companies.name ASC, contacts.full_name ASC LIMIT 0, 50

As you can see the "companies" table does not get added into the FROM
clause in the generated SQL statement.

4. Trying to add the following to Contacts controller has no effect:
config.columns[:company].includes = [:company]

What is the expected output? What do you see instead?
Expect a nested list of contacts for specific company, but get an SQL error
notifying me that companies.name is an invalid column as companies table is
not included into the FROM clause.

What version (or revision) of the product are you using?
Rails 2.3.5, latest AS master from github (as of 2009-12-03) with
render_component pluging.

If this bug causes an exception, please paste at least the first 20 lines
below.
There is no App trace (just framework and full trace).

Original issue reported on code.google.com by netv...@gmail.com on 15 Dec 2009 at 1:51

GoogleCodeExporter commented 9 years ago
In ContactsController, define joins_for_collection if you want to add a join (as
joins option of AR::find), or add your association to active_scaffold_joins if 
you
want to add an include

Original comment by sergio.c...@gmail.com on 15 Dec 2009 at 2:20

GoogleCodeExporter commented 9 years ago
Also, you can change config.list.sorting in a before_filter to order only by
full_name when is nested.

Original comment by sergio.c...@gmail.com on 15 Dec 2009 at 2:23

GoogleCodeExporter commented 9 years ago
Hi Sergio,

Sorry to be so helpless.
Would you mind providing some code examples for both proposed solutions?
Thank you!

Original comment by netv...@gmail.com on 15 Dec 2009 at 10:01

GoogleCodeExporter commented 9 years ago
For the before_filter solution, I have created a filter like this:

private
def adjust_sorting
 if params.has_key?(:nested)
  active_scaffold_config.list.sorting = [{ :full_name => :asc }]
 else
  active_scaffold_config.list.sorting = [{:company => :asc}, { :full_name => :asc }]
 end
end

This works. However I'm wondering if there is better solution? I noticed that
has_key? method is deprecated in the latest Rails as per this:
http://apidock.com/rails/Rack/Utils/HeaderHash/has_key%3F

Original comment by netv...@gmail.com on 15 Dec 2009 at 10:25

GoogleCodeExporter commented 9 years ago
I'm not sure params is a HeaderHash, anyway you can try with params[:nested] 
too.

Original comment by sergio.c...@gmail.com on 16 Dec 2009 at 8:49

GoogleCodeExporter commented 9 years ago
Hmmm... you're correct params is actually a HashWithIndifferentAccess...
Which inherits Hash's methods including has_key?.
But somewhere along the lines as I was testing ways to fix this issue, I got a
deprecation notice about has_key?, after a bit of googling I got to that link.
In any case I have it solved with the before_filter as above.

However, I'm interested in the joins_for_collection way of solving this.
Would you care to post a wee bit more code. I'm coming up short with google for
actual examples that would be relevant. 

Thank you in advance!

Original comment by netv...@gmail.com on 16 Dec 2009 at 9:22