rails/rails — Issue #4562: Bad ActiveRecord has_many behavior for custom SQL when counting
Author: NoICE
Created: 2012-01-20 21:39:39+00:00
State: open

``` ruby
has_many :rights, :class_name => 'User::Right',
:finder_sql => proc { "SELECT p.* FROM user_tbl_right AS p
JOIN user_tbl_group_right ON gr_right = r_id
JOIN user_tbl_member ON m_group = gr_group
WHERE m_user = #{u_id}" }
```

produces this:

ActiveRecord::StatementInvalid (Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* ) FROM user_tbl_right AS p
JOIN user_tbl_group_right ON gr' at line 1: SELECT COUNT( p.* ) FROM user_tbl_right AS p
JOIN user_tbl_group_right ON gr_right = r_id
JOIN user_tbl_member ON m_group = gr_group
WHERE m_user = 1):

using rails 3.2.0 and mysql 0.3.11.

Had to fix it temporarily in my application via replacing `p.*` to `*` ...