Skip to content

Commit

Permalink
Fix ActiveRecord::StatementInvalid error when using UUID as primary key
Browse files Browse the repository at this point in the history
  • Loading branch information
calvertyang committed Jun 2, 2018
1 parent a0a1a7d commit 442b429
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/acts_as_taggable_on/taggable/related.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def related_tags_for(context, klass, options = {})
private

def exclude_self(klass, id)
"#{klass.table_name}.#{klass.primary_key} != #{id} AND" if [self.class.base_class, self.class].include? klass
"#{klass.arel_table[klass.primary_key].not_eq(id).to_sql} AND" if [self.class.base_class, self.class].include? klass
end

def group_columns(klass)
Expand Down
9 changes: 9 additions & 0 deletions spec/acts_as_taggable_on/related_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@
expect(taggable1.find_related_tags).to_not include(taggable2)
end

it 'should find related objects based on tag names on context - uuid primary key' do
taggable1 = TaggableModelWithUuidPrimaryKey.create!(name: 'Taggable 1',tag_list: 'one, two')
taggable2 = TaggableModelWithUuidPrimaryKey.create!(name: 'Taggable 2',tag_list: 'three, four')
taggable3 = TaggableModelWithUuidPrimaryKey.create!(name: 'Taggable 3',tag_list: 'one, four')

expect(taggable1.find_related_tags).to include(taggable3)
expect(taggable1.find_related_tags).to_not include(taggable2)
end

it 'should find other related objects based on tag names on context' do
taggable1 = TaggableModel.create!(name: 'Taggable 1',tag_list: 'one, two')
taggable2 = OtherTaggableModel.create!(name: 'Taggable 2',tag_list: 'three, four')
Expand Down
9 changes: 9 additions & 0 deletions spec/internal/app/models/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ class NonStandardIdTaggableModel < ActiveRecord::Base
has_many :untaggable_models
end

class TaggableModelWithUuidPrimaryKey < ActiveRecord::Base
self.primary_key = :an_id
acts_as_taggable
acts_as_taggable_on :languages
acts_as_taggable_on :skills
acts_as_taggable_on :needs, :offerings
has_many :untaggable_models
end

class OrderedTaggableModel < ActiveRecord::Base
acts_as_ordered_taggable
acts_as_ordered_taggable_on :colours
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class TaggableModelWithUuidPrimaryKey < ActiveRecord::Base
acts_as_taggable
acts_as_taggable_on :languages
acts_as_taggable_on :skills
acts_as_taggable_on :needs, :offerings
has_many :untaggable_models
end
8 changes: 8 additions & 0 deletions spec/internal/db/schema.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
ActiveRecord::Schema.define version: 0 do
enable_extension 'uuid-ossp' unless extension_enabled?('uuid-ossp')
enable_extension 'pgcrypto' if using_postgresql? && !extension_enabled?('pgcrypto')

create_table :tags, force: true do |t|
t.string :name
t.integer :taggings_count, default: 0
Expand Down Expand Up @@ -41,6 +44,11 @@
t.column :type, :string
end

create_table :taggable_model_with_uuid_primary_keys, id: :uuid, force: true do |t|
t.column :name, :string
t.column :type, :string
end

create_table :untaggable_models, force: true do |t|
t.column :taggable_model_id, :integer
t.column :name, :string
Expand Down

0 comments on commit 442b429

Please sign in to comment.