Defect #11073

UserCustomField order_statement returns wrong output

Added by Devadatta Sahoo over 5 years ago. Updated over 5 years ago.

Status:ClosedStart date:
Priority:NormalDue date:
Assignee:Etienne Massip% Done:

0%

Category:Custom fields
Target version:2.1.0
Resolution:Fixed Affected version:

Description

The UserCustomField order_statement returns wrong output:

Eg:
For a user custom field named "Employee Code" (type string) the order statement is returned as follows:

>> UserCustomField.find_by_name("Employee Code").order_statement
=> "COALESCE((SELECT cv_sort.value FROM custom_values cv_sort WHERE cv_sort.customized_type='User' AND cv_sort.customized_id=users.id AND cv_sort.custom_field_id=1 LIMIT 1), '')"

Herein the value of customized_type is User, whereas in the actual database table, i.e. custom_values table, the value of customized_type for UserCustomFields is Principal (which is a superclass of User class).

For this reason, the `UserCustomField.find_by_name("Employee Code").order_statement` gives a statement which always returns empty output when used in a query.

The correct value of `UserCustomField.find_by_name("Employee Code").order_statement` should be:

>> UserCustomField.find_by_name("Employee Code").order_statement
=> "COALESCE((SELECT cv_sort.value FROM custom_values cv_sort WHERE cv_sort.customized_type='Principal' AND cv_sort.customized_id=users.id AND cv_sort.custom_field_id=1 LIMIT 1), '')"

custom_field.rb.patch Magnifier (1.64 KB) Devadatta Sahoo, 2012-06-01 20:06


Related issues

Related to Redmine - Feature #1139: Being able to sort the issue list by custom fields Closed 2008-04-29

Associated revisions

Revision 9880
Added by Etienne Massip over 5 years ago

Use base class name as customized type to fix UserCustomField#order_statement.

Revision 9881
Added by Etienne Massip over 5 years ago

Removed test line committed accidently (#11073).

History

#1 Updated by Devadatta Sahoo over 5 years ago

  • Assignee set to Jean-Philippe Lang

#2 Updated by Etienne Massip over 5 years ago

  • Assignee deleted (Jean-Philippe Lang)
  • Priority changed from High to Normal

#3 Updated by Devadatta Sahoo over 5 years ago

A suggested patch has been attached:

File to be patched:

app/models/custom_field.rb

#4 Updated by Etienne Massip over 5 years ago

  • Status changed from New to Confirmed
  • Target version set to Candidate for next minor release

Rails stores the base class type (Principal) instead of the actual class type (User).

These RoR issues might be relevant:

https://rails.lighthouseapp.com/projects/8994/tickets/5617
https://github.com/rails/rails/issues/724 (continuation of lh 5617)
https://github.com/rails/rails/issues/617

#5 Updated by Etienne Massip over 5 years ago

Maybe you can try to replace self.class.customized_class.name with self.class.customized_class.base_class.name?

#6 Updated by Etienne Massip over 5 years ago

  • Status changed from Confirmed to Resolved
  • Assignee set to Etienne Massip
  • Target version changed from Candidate for next minor release to 2.1.0
  • Resolution set to Fixed

Should be fixed with r9880, thanks for the report.

I don't know if it's useful to write a test since this statement is not used in Redmine code to query User model nor where I should write it?

#7 Updated by Jean-Philippe Lang over 5 years ago

  • Status changed from Resolved to Closed

Also available in: Atom PDF