diff --git a/lib/hstore_accessor/macro.rb b/lib/hstore_accessor/macro.rb index 01b5eed..cd9dd6d 100644 --- a/lib/hstore_accessor/macro.rb +++ b/lib/hstore_accessor/macro.rb @@ -115,8 +115,8 @@ def hstore_accessor(hstore_attribute, fields) end end - query_field = "#{hstore_attribute} -> '#{store_key}'" - eq_query_field = "#{hstore_attribute} @> hstore('#{store_key}', ?)" + query_field = "#{table_name}.#{hstore_attribute} -> '#{store_key}'" + eq_query_field = "#{table_name}.#{hstore_attribute} @> hstore('#{store_key}', ?)" case data_type when :string diff --git a/spec/hstore_accessor_spec.rb b/spec/hstore_accessor_spec.rb index 62dd298..42d1b9b 100644 --- a/spec/hstore_accessor_spec.rb +++ b/spec/hstore_accessor_spec.rb @@ -2,6 +2,7 @@ require "active_support/all" FIELDS = { + name: :string, color: :string, price: :integer, published: { data_type: :boolean, store_key: "p" }, @@ -9,6 +10,7 @@ popular: :boolean, build_timestamp: :datetime, released_at: :date, + likes: :integer, miles: :decimal } @@ -16,9 +18,15 @@ color_data: :string } +class ProductCategory < ActiveRecord::Base + hstore_accessor :options, name: :string, likes: :integer + has_many :products +end + class Product < ActiveRecord::Base hstore_accessor :options, FIELDS hstore_accessor :data, DATA_FIELDS + belongs_to :product_category end class SuperProduct < Product @@ -145,10 +153,35 @@ class FakeModel describe "scopes" do let!(:timestamp) { Time.now } let!(:datestamp) { Date.today } - let!(:product_a) { Product.create(color: "green", price: 10, weight: 10.1, popular: true, build_timestamp: (timestamp - 10.days), released_at: (datestamp - 8.days), miles: BigDecimal.new("10.113379001")) } + let!(:product_a) { Product.create(likes: 3, name: "widget", color: "green", price: 10, weight: 10.1, popular: true, build_timestamp: (timestamp - 10.days), released_at: (datestamp - 8.days), miles: BigDecimal.new("10.113379001")) } let!(:product_b) { Product.create(color: "orange", price: 20, weight: 20.2, popular: false, build_timestamp: (timestamp - 5.days), released_at: (datestamp - 4.days), miles: BigDecimal.new("20.213379001")) } let!(:product_c) { Product.create(color: "blue", price: 30, weight: 30.3, popular: true, build_timestamp: timestamp, released_at: datestamp, miles: BigDecimal.new("30.313379001")) } + context "ambiguous column names" do + let!(:product_category) { ProductCategory.create!(name: "widget", likes: 2) } + + before do + product_category.products = Product.all + product_category.save! + end + + context "eq query" do + let!(:query) { Product.all.joins(:product_category).merge(ProductCategory.with_name("widget")).with_name("widget") } + + it "qualifies the table name to prevent ambiguous column name references" do + expect { query.to_a }.to_not raise_error + end + end + + context "query" do + let!(:query) { Product.all.joins(:product_category).merge(ProductCategory.likes_lt(4)).likes_lt(4) } + + it "qualifies the table name to prevent ambiguous column name references" do + expect { query.to_a }.to_not raise_error + end + end + end + context "for string fields support" do it "equality" do expect(Product.with_color("orange").to_a).to eq [product_b] diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e09b6d8..ce54845 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -26,6 +26,7 @@ def create_database ActiveRecord::Base.connection.execute("CREATE EXTENSION hstore;") rescue ActiveRecord::StatementInvalid ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS products;") + ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS product_categories;") ActiveRecord::Base.connection.create_table(:products) do |t| t.hstore :options @@ -33,6 +34,7 @@ def create_database t.string :string_type t.integer :integer_type + t.integer :product_category_id t.boolean :boolean_type t.float :float_type t.time :time_type @@ -40,4 +42,8 @@ def create_database t.datetime :datetime_type t.decimal :decimal_type end + + ActiveRecord::Base.connection.create_table(:product_categories) do |t| + t.hstore :options + end end