hi! having the following models,
Contact
has_many :group_contact_classifications
has_many :groups, :through => :group_contact_classifications
GroupContactClassification
belongs_to :group
belongs_to :contact
Group
has_many :group_contact_classifications
has_many :contacts, :through => :group_contact_classifications
i just want to convert the following mysql command to active record
SELECT *, GROUP_CONCAT(g.name SEPARATOR " ") AS grp_names
FROM contacts
LEFT OUTER JOIN group_contact_classifications gcc ON gcc.contact_id = contacts.id
LEFT INNER JOIN groups g ON g.id = gcc.group_id
GROUP BY contacts.id
ORDER BY grp_names;
it's not
Contact.all
:select => 'id, group_concat(g.name separator " ") as grp_names',
:include => :groups,
:order => 'grp_names'
the error raised by mysql is Mysql::Error: Unknown column 'grp_names'
If you're wondering what I'm trying to achieve, I want to sort the contacts by the groups they belong to like the output of the sql statement above.
+------+-----------+
| id | grp_names |
+------+-----------+
| 212 | Bar |
| 826 | Foo |
| 1310 | Foo |
| 2226 | Foo Bar |
+------+-----------+