I am using Rails 2.1 with an Oracle database on OSX.
I have a UserProfile model with a has_and_belongs_to_many association of UserRoles.
I executed the following:
UserProfile.find(1, :include => :user_roles)
I got this error:
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:147:in `log': OCIError: ORA-00911: invalid character: SELECT user_role.*, t0.user_profile_id as _parent_record_id FROM user_role INNER JOIN user_profile_user_role as t0 ON user_role.id = t0.user_role_id WHERE (t0.user_profile_id IN (1)) (ActiveRecord::StatementInvalid)
from /Library/Ruby/Gems/1.8/gems/activerecord-oracle-adapter-1.0.0.9216/lib/active_record/connection_adapters/oracle_adapter.rb:221:in `execute'
from /Library/Ruby/Gems/1.8/gems/activerecord-oracle-adapter-1.0.0.9216/lib/active_record/connection_adapters/oracle_adapter.rb:516:in `select'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract/query_cache.rb:61:in `select_all'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:582:in `find_by_sql'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1341:in `find_every'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:536:in `find_without_pagination'
from /Users/mark/src/msd/vendor/plugins/paginating_find/lib/paginating_find.rb:102:in `find'
... 16 levels...
from /Library/Ruby/Gems/1.8/gems/rails-2.1.0/lib/commands/runner.rb:47
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in `require'
from ./script/runner:3
I pasted this SQL into Oracle's SQL Developer and played with it there to work out what was wrong. There were actually two different problems.
The first syntax problem was the "as" in the inner join to user_profile_user_role. I patched the code to remove this "as" and got a second error. This time it was the column alias "_parent_record_id". In Oracle a name cannot start with an underscore. I replaced "_parent_record_id" with "x_parent_record_id" and things worked.