This project is archived and is in readonly mode.

#1461 ✓resolved
Joe Rafaniello

has_many with conditions in hash format generates invalid syntax

Reported by Joe Rafaniello | November 24th, 2008 @ 09:15 PM | in 2.x

Environment: Rails 2.2.2 and Postgres database.

A simple has_many relationship works in rails 2.1.2 but fails due to invalid sql syntax in 2.2.2.

The failing has_many: has_many :keys, :foreign_key => "keyboard_id", :conditions => {:position => 'pressed'}, :dependent => :nullify # fails

This has_many does not generate the with invalid syntax error: has_many :keys, :foreign_key => "keyboard_id", :conditions => "position = 'pressed'", :dependent => :nullify # works

Below is the output. Attached is a very simple project with 2 models. The output.log is in the root of the tgz.

=> Booting Mongrel (use 'script/server webrick' to force WEBrick) => Rails 2.2.2 application starting on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server Starting Mongrel listening at 0.0.0.0:3000 Starting Rails with production environment... Exiting /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations.rb:1476:in configure_dependency_for_has_many': (eval):9:inconfigure_dependency_for_has_many': compile error (SyntaxError) (eval):7: syntax error, unexpected tIDENTIFIER, expecting ')'

                  "(keyboard_id = #{record.quoted_id}) AND ("keyboards"."position" = E'pressed')")
                                                                      ^

(eval):7: syntax error, unexpected tIDENTIFIER, expecting kEND

                  "(keyboard_id = #{record.quoted_id}) AND ("keyboards"."position" = E'pressed')")
                                                                                 ^

(eval):7: syntax error, unexpected ')', expecting kEND from /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations.rb:768:in has_many'

from /home/jrafaniello/git_src/test_project/app/models/keyboard.rb:2
from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from /usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:155:in `require'
from /usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:262:in `require_or_load'
from /usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:221:in `depend_on'
from /usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:133:in `require_dependency'
 ... 35 levels...
from /usr/lib/ruby/gems/1.8/gems/rails-2.2.2/lib/commands/server.rb:49
from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from script/server:3

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Attachments

Referenced by

Pages