Permalink
Browse files

add the primary table name to all fields in the table to cut down on …

…collisions
  • Loading branch information...
1 parent aac235e commit 8b85254a6777c87a3ae54d9beda78d380aa5aa38 Aaron Lasseigne committed Feb 3, 2012
@@ -30,7 +30,13 @@ module ActiveRecordModelExtension
if options[:fields] and options[:fields][relation.current_order[:field]]
order_text = options[:fields][relation.current_order[:field]]
else
- order_text = "#{relation.connection.quote_column_name(relation.current_order[:field])} ASC"
+ field = relation.current_order[:field].to_s
+
+ if field !~ /\./ and relation.column_names.include?(field)
+ field = "#{self.table_name}.#{field}"
+ end
+
+ order_text = "#{relation.connection.quote_column_name(field)} ASC"
end
if relation.current_order[:dir].try(:downcase) == 'desc'
@@ -5,32 +5,49 @@
context 'orders using a field' do
it 'where the order is a hash containing :order' do
npw = NobelPrizeWinner.with_order({order: 'first_name-asc'})
- npw.order_values.should == ['"first_name" ASC']
+ npw.order_values.should == ['"nobel_prize_winners.first_name" ASC']
npw.reverse_order_value.should == nil
end
it 'where the order is a symbol' do
npw = NobelPrizeWinner.with_order(:first_name)
- npw.order_values.should == ['"first_name" ASC']
+ npw.order_values.should == ['"nobel_prize_winners.first_name" ASC']
npw.reverse_order_value.should == nil
end
it 'where the order is a string' do
npw = NobelPrizeWinner.with_order('first_name-asc')
- npw.order_values.should == ['"first_name" ASC']
+ npw.order_values.should == ['"nobel_prize_winners.first_name" ASC']
npw.reverse_order_value.should == nil
end
end
+ context 'limit the need for specifying table names to resolve ambiguity' do
+ it 'prepends the table name to the field if the field is in the primary table' do
+ npw = NobelPrizeWinner.joins(:nobel_prizes).with_order(:id)
+ npw.order_values.should == ['"nobel_prize_winners.id" ASC']
+ end
+
+ it 'skips fields where a table is already provided' do
+ npw = NobelPrizeWinner.joins(:nobel_prizes).with_order('nobel_prizes.year')
+ npw.order_values.should == ['"nobel_prizes.year" ASC']
+ end
+
+ it 'does not affect non-primary fields' do
+ npw = NobelPrizeWinner.joins(:nobel_prizes).with_order(:year)
+ npw.order_values.should == ['"year" ASC']
+ end
+ end
+
it 'reverses order using a field' do
npw = NobelPrizeWinner.with_order({order: 'first_name-desc'})
- npw.order_values.should == ['"first_name" ASC']
+ npw.order_values.should == ['"nobel_prize_winners.first_name" ASC']
npw.reverse_order_value.should be true
end
it 'does not break the chain' do
npw = NobelPrizeWinner.with_order({order: 'first_name-asc'}).limit(1)
- npw.order_values.should == ['"first_name" ASC']
+ npw.order_values.should == ['"nobel_prize_winners.first_name" ASC']
npw.reverse_order_value.should == nil
end
@@ -44,7 +61,7 @@
context 'params do not include a direction' do
it 'defaults to "ASC"' do
npw = NobelPrizeWinner.with_order({order: 'first_name'})
- npw.order_values.should == ['"first_name" ASC']
+ npw.order_values.should == ['"nobel_prize_winners.first_name" ASC']
npw.reverse_order_value.should == nil
end
end
@@ -74,13 +91,13 @@
context 'options include :default to select a default field to order by' do
it 'should default to the field when no :order param is passed' do
npw = NobelPrizeWinner.with_order({}, {default: 'first_name'})
- npw.order_values.should == ['"first_name" ASC']
+ npw.order_values.should == ['"nobel_prize_winners.first_name" ASC']
npw.reverse_order_value.should == nil
end
it 'should not default to the field when a :order param is passed' do
npw = NobelPrizeWinner.with_order({order: 'first_name'}, {default: 'last_name'})
- npw.order_values.should == ['"first_name" ASC']
+ npw.order_values.should == ['"nobel_prize_winners.first_name" ASC']
npw.reverse_order_value.should == nil
end
end
@@ -0,0 +1,3 @@
+class NobelPrize < ActiveRecord::Base
+ belongs_to :nobel_prize_winner
+end
@@ -1,2 +1,3 @@
class NobelPrizeWinner < ActiveRecord::Base
+ has_many :nobel_prizes
end
@@ -0,0 +1,34 @@
+class CreateNobelPrizes < ActiveRecord::Migration
+ def up
+ create_table :nobel_prizes do |t|
+ t.integer :nobel_prize_winner_id
+ t.string :category
+ t.integer :year
+ end
+
+ execute(
+ 'INSERT INTO nobel_prizes("nobel_prize_winner_id", "category", "year") ' +
+ 'SELECT id, category, year ' +
+ 'FROM nobel_prize_winners'
+ )
+
+ NobelPrizeWinner.find_by_first_name_and_last_name('Marie', 'Curie').nobel_prizes.create(category: 'Physics', year: 1903)
+
+ remove_column :nobel_prize_winners, :category, :year
+ end
+
+ def down
+ add_column :nobel_prize_winners, :category, :string
+ add_column :nobel_prize_winners, :year, :integer
+
+ NobelPrizeWinner.find_by_first_name_and_last_name('Marie', 'Curie').nobel_prizes.order(:year).first.delete
+
+ execute(
+ 'UPDATE nobel_prize_winners ' +
+ 'SET category = (SELECT nobel_prizes.category FROM nobel_prizes WHERE nobel_prize_winners.id = nobel_prizes.nobel_prize_winner_id), ' +
+ 'year = (SELECT nobel_prizes.year FROM nobel_prizes WHERE nobel_prize_winners.id = nobel_prizes.nobel_prize_winner_id)'
+ )
+
+ drop_table :nobel_prizes
+ end
+end
View
@@ -11,11 +11,15 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120127203225) do
+ActiveRecord::Schema.define(:version => 20120203212237) do
create_table "nobel_prize_winners", :force => true do |t|
- t.string "first_name"
- t.string "last_name"
+ t.string "first_name"
+ t.string "last_name"
+ end
+
+ create_table "nobel_prizes", :force => true do |t|
+ t.integer "nobel_prize_winner_id"
t.string "category"
t.integer "year"
end
View
Binary file not shown.
View
@@ -42,3 +42,94 @@ Migrating to CreateNobelPrizeWinners (20120127203225)
NobelPrizeWinner Load (0.2ms) SELECT "nobel_prize_winners".* FROM "nobel_prize_winners" 
NobelPrizeWinner Load (0.1ms) SELECT "nobel_prize_winners".* FROM "nobel_prize_winners"
NobelPrizeWinner Load (0.1ms) SELECT "nobel_prize_winners".* FROM "nobel_prize_winners" 
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
+Migrating to CreateNobelPrizeWinners (20120127203225)
+Migrating to CreateNobelPrizes (20120203212237)
+  (0.0ms) select sqlite_version(*)
+  (0.0ms) begin transaction
+  (0.4ms) CREATE TABLE "nobel_prizes" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "nobel_prize_winner_id" integer, "category" varchar(255), "year" integer)
+  (0.1ms) INSERT INTO nobel_prizes("nobel_prize_winner_id", "category", "year") SELECT id, category, year FROM nobel_prize_winners
+ NobelPrizeWinner Load (0.1ms) SELECT "nobel_prize_winners".* FROM "nobel_prize_winners" WHERE "nobel_prize_winners"."first_name" = 'Marie' AND "nobel_prize_winners"."last_name" = 'Curie' LIMIT 1
+ SQL (13.1ms) INSERT INTO "nobel_prizes" ("category", "nobel_prize_winner_id", "year") VALUES (?, ?, ?) [["category", "Physics"], ["nobel_prize_winner_id", 10], ["year", 1903]]
+  (0.4ms) CREATE TEMPORARY TABLE "altered_nobel_prize_winners" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "first_name" varchar(255), "last_name" varchar(255), "category" varchar(255), "year" integer)
+  (0.0ms) PRAGMA index_list("nobel_prize_winners")
+  (0.1ms) SELECT * FROM "nobel_prize_winners"
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","category","year") VALUES (1, 'Norman', 'Borlaug', 'Peace', 1970)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","category","year") VALUES (2, 'Paul', 'Flory', 'Chemistry', 1974)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","category","year") VALUES (3, 'Albert', 'Eintein', 'Physics', 1921)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","category","year") VALUES (4, 'Samuel', 'Beckett', 'Literature', 1969)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","category","year") VALUES (5, 'Niels', 'Bohr', 'Physics', 1922)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","category","year") VALUES (6, 'Erwin', 'Schrodinger', 'Physics', 1933)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","category","year") VALUES (7, 'Paul', 'Dirac', 'Physics', 1933)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","category","year") VALUES (8, 'Enrico', 'Fermi', 'Physics', 1938)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","category","year") VALUES (9, 'Richard', 'Feynman', 'Physics', 1965)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","category","year") VALUES (10, 'Marie', 'Curie', 'Chemistry', 1911)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","category","year") VALUES (11, 'James', 'Watson', 'Physiology or Medicine', 1962)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","category","year") VALUES (12, 'Bertrand', 'Russell', 'Literature', 1950)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","category","year") VALUES (13, 'John', 'Steinbeck', 'Literature', 1962)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","category","year") VALUES (14, 'Nelson', 'Mandela', 'Peace', 1993)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","category","year") VALUES (15, 'Jacques', 'Monod', 'Physiology or Medicine', 1965)
+  (0.3ms) DROP TABLE "nobel_prize_winners"
+  (0.1ms) CREATE TABLE "nobel_prize_winners" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "first_name" varchar(255), "last_name" varchar(255), "year" integer) 
+  (0.0ms) PRAGMA index_list("altered_nobel_prize_winners")
+  (0.1ms) SELECT * FROM "altered_nobel_prize_winners"
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name","year") VALUES (1, 'Norman', 'Borlaug', 1970)
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name","year") VALUES (2, 'Paul', 'Flory', 1974)
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name","year") VALUES (3, 'Albert', 'Eintein', 1921)
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name","year") VALUES (4, 'Samuel', 'Beckett', 1969)
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name","year") VALUES (5, 'Niels', 'Bohr', 1922)
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name","year") VALUES (6, 'Erwin', 'Schrodinger', 1933)
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name","year") VALUES (7, 'Paul', 'Dirac', 1933)
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name","year") VALUES (8, 'Enrico', 'Fermi', 1938)
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name","year") VALUES (9, 'Richard', 'Feynman', 1965)
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name","year") VALUES (10, 'Marie', 'Curie', 1911)
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name","year") VALUES (11, 'James', 'Watson', 1962)
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name","year") VALUES (12, 'Bertrand', 'Russell', 1950)
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name","year") VALUES (13, 'John', 'Steinbeck', 1962)
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name","year") VALUES (14, 'Nelson', 'Mandela', 1993)
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name","year") VALUES (15, 'Jacques', 'Monod', 1965)
+  (0.3ms) DROP TABLE "altered_nobel_prize_winners"
+  (0.1ms) CREATE TEMPORARY TABLE "altered_nobel_prize_winners" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "first_name" varchar(255), "last_name" varchar(255), "year" integer)
+  (0.0ms) PRAGMA index_list("nobel_prize_winners")
+  (0.1ms) SELECT * FROM "nobel_prize_winners"
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","year") VALUES (1, 'Norman', 'Borlaug', 1970)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","year") VALUES (2, 'Paul', 'Flory', 1974)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","year") VALUES (3, 'Albert', 'Eintein', 1921)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","year") VALUES (4, 'Samuel', 'Beckett', 1969)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","year") VALUES (5, 'Niels', 'Bohr', 1922)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","year") VALUES (6, 'Erwin', 'Schrodinger', 1933)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","year") VALUES (7, 'Paul', 'Dirac', 1933)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","year") VALUES (8, 'Enrico', 'Fermi', 1938)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","year") VALUES (9, 'Richard', 'Feynman', 1965)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","year") VALUES (10, 'Marie', 'Curie', 1911)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","year") VALUES (11, 'James', 'Watson', 1962)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","year") VALUES (12, 'Bertrand', 'Russell', 1950)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","year") VALUES (13, 'John', 'Steinbeck', 1962)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","year") VALUES (14, 'Nelson', 'Mandela', 1993)
+  (0.0ms) INSERT INTO "altered_nobel_prize_winners" ("id","first_name","last_name","year") VALUES (15, 'Jacques', 'Monod', 1965)
+  (0.1ms) DROP TABLE "nobel_prize_winners"
+  (0.1ms) CREATE TABLE "nobel_prize_winners" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "first_name" varchar(255), "last_name" varchar(255)) 
+  (0.0ms) PRAGMA index_list("altered_nobel_prize_winners")
+  (0.1ms) SELECT * FROM "altered_nobel_prize_winners"
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name") VALUES (1, 'Norman', 'Borlaug')
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name") VALUES (2, 'Paul', 'Flory')
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name") VALUES (3, 'Albert', 'Eintein')
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name") VALUES (4, 'Samuel', 'Beckett')
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name") VALUES (5, 'Niels', 'Bohr')
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name") VALUES (6, 'Erwin', 'Schrodinger')
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name") VALUES (7, 'Paul', 'Dirac')
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name") VALUES (8, 'Enrico', 'Fermi')
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name") VALUES (9, 'Richard', 'Feynman')
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name") VALUES (10, 'Marie', 'Curie')
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name") VALUES (11, 'James', 'Watson')
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name") VALUES (12, 'Bertrand', 'Russell')
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name") VALUES (13, 'John', 'Steinbeck')
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name") VALUES (14, 'Nelson', 'Mandela')
+  (0.0ms) INSERT INTO "nobel_prize_winners" ("id","first_name","last_name") VALUES (15, 'Jacques', 'Monod')
+  (0.1ms) DROP TABLE "altered_nobel_prize_winners"
+  (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES ('20120203212237')
+  (228.9ms) commit transaction
+  (0.2ms) select sqlite_version(*)
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
+  (0.0ms) PRAGMA index_list("nobel_prize_winners")
+  (0.0ms) PRAGMA index_list("nobel_prizes")

0 comments on commit 8b85254

Please sign in to comment.