jruby/activerecord-jdbc-adapter — Issue #158: Update with join using MS SQL Server?
Author: msernatinger
Created: 2012-01-25 20:31:25+00:00
State: open

Hi all,

I'm using JRuby 1.6.4 with Rails 3.0.8. I have a pretty straightforward query: Assume a Bird model that belongs_to a Flock model. I want to update all birds where their flock meets a certain criteria. So I have:

Bird.joins(:flock).update_all(..updates..., ...conditions...).

But this produces the following error:

ActiveRecord::JDBCError: Incorrect syntax near the keyword 'INNER'.: UPDATE [birds] INNER JOIN [flocks] ON [flocks].[id] = [birds.[flock_id] SET [feather] = N'black' WHERE (flock.range = N'500 miles')

A correct version of this query would look like

UPDATE [birds] SET [feather] = N'black' FROM [birds] INNER JOIN [flock] ON [flock].[id] = [bird].[flock_id] WHERE (flock.range = N'500 miles')

This works as it should in MySQL.