Skip to content

Instantly share code, notes, and snippets.

@jeremy
Created April 11, 2014 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremy/10507535 to your computer and use it in GitHub Desktop.
Save jeremy/10507535 to your computer and use it in GitHub Desktop.
require 'active_record'
ActiveRecord::Base.logger = Logger.new($stdout)
ActiveRecord::Base.establish_connection adapter: 'mysql2', database: 'activerecord_unittest'
ActiveRecord::Base.connection.instance_eval do
create_table(:taggings, :force => true) do |t|
t.belongs_to :taggable, polymorphic: true
t.belongs_to :tag
end
create_table(:tags, :force => true) { |t| t.string :name }
create_table(:people, :force => true) { |t| t.string :name }
end
class Tagging < ActiveRecord::Base
belongs_to :tag
belongs_to :taggable, polymorphic: true
end
class Tag < ActiveRecord::Base
has_many :taggings, as: :taggable
end
class Person < ActiveRecord::Base
scope :tagged_with, ->(id) { joins(:taggings).where(taggings: { tag_id: id }) }
has_many :taggings, as: :taggable
has_many :tags, through: :taggings
end
tag = Tag.create! name: 't'
p = Person.create! name: 'p'
Tagging.create! tag: tag, taggable: p
Person.tagged_with(tag.id).update_all name: "rofl"
@jeremy
Copy link
Author

jeremy commented Apr 11, 2014

sqlite3:

D, [2014-04-09T17:55:13.059445 #9393] DEBUG -- :   SQL (0.2ms)  UPDATE "people" SET name="rofl" WHERE "people"."id" IN (SELECT "people"."id" FROM "people" INNER JOIN "taggings" ON "taggings"."taggable_id" = "people"."id" AND "taggings"."taggable_type" = ? WHERE "taggings"."tag_id" = 1)

mysql2:

D, [2014-04-09T18:08:07.999026 #10217] DEBUG -- :   SQL (0.2ms)  UPDATE `people` INNER JOIN `taggings` ON `taggings`.`taggable_id` = `people`.`id` AND `taggings`.`taggable_type` = SET `people`.`name` = 'rofl' WHERE `taggings`.`tag_id` = 1
E, [2014-04-09T18:08:07.999089 #10217] ERROR -- : 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 'SET `people`.`name` = 'rofl' WHERE `taggings`.`tag_id` = 1' at line 1: UPDATE `people` INNER JOIN `taggings` ON `taggings`.`taggable_id` = `people`.`id` AND `taggings`.`taggable_type` = SET `people`.`name` = 'rofl' WHERE `taggings`.`tag_id` = 1
/Users/jeremy/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/bundler/gems/rails-6388b4b15383/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:320:in `query': 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 'SET `people`.`name` = 'rofl' WHERE `taggings`.`tag_id` = 1' at line 1: UPDATE `people` INNER JOIN `taggings` ON `taggings`.`taggable_id` = `people`.`id` AND `taggings`.`taggable_type` = SET `people`.`name` = 'rofl' WHERE `taggings`.`tag_id` = 1 (ActiveRecord::StatementInvalid)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment