Skip to content

Commit

Permalink
added table names to scopes to prevent ambiguous column errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Crismali committed Mar 16, 2015
1 parent ec137a3 commit eae9469
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/hstore_accessor/macro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 34 additions & 1 deletion spec/hstore_accessor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,31 @@
require "active_support/all"

FIELDS = {
name: :string,
color: :string,
price: :integer,
published: { data_type: :boolean, store_key: "p" },
weight: { data_type: :float, store_key: "w" },
popular: :boolean,
build_timestamp: :datetime,
released_at: :date,
likes: :integer,
miles: :decimal
}

DATA_FIELDS = {
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
Expand Down Expand Up @@ -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]
Expand Down
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,24 @@ 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
t.hstore :data

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
t.date :date_type
t.datetime :datetime_type
t.decimal :decimal_type
end

ActiveRecord::Base.connection.create_table(:product_categories) do |t|
t.hstore :options
end
end

0 comments on commit eae9469

Please sign in to comment.