Skip to content

Instantly share code, notes, and snippets.

@hannahwhy
Created October 13, 2010 16:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hannahwhy/624421 to your computer and use it in GitHub Desktop.
Save hannahwhy/624421 to your computer and use it in GitHub Desktop.
exists? with default_scope error (AR >= 2.3.9)
# Logfile created on Wed Oct 13 11:50:35 -0500 2010 by logger.rb/22285
SQL (0.3ms) select sqlite_version(*)
SQL (0.5ms) CREATE TABLE "widgets" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "accessory_id" integer)
SQL (0.1ms) CREATE TABLE "accessories" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)
Accessory Create (0.1ms) INSERT INTO accessories VALUES(NULL)
Widget Create (0.1ms) INSERT INTO "widgets" ("accessory_id") VALUES(1)
Widget Load (0.1ms) SELECT "widgets".id FROM "widgets" WHERE ("widgets"."accessory_id" = 1) LIMIT 1
gem 'activerecord', '2.3.9'
require 'active_record'
require 'pp'
ActiveRecord::Base.logger = Logger.new('exists_with_scope.log')
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
ActiveRecord::Base.connection.create_table :widgets do |t|
t.references :accessory
end
ActiveRecord::Base.connection.create_table :accessories do |t|
end
class Widget < ActiveRecord::Base
belongs_to :accessory
default_scope :include => :accessory
end
class Accessory < ActiveRecord::Base
end
a1 = Accessory.create
Widget.create(:accessory_id => a1.id)
pp Widget.columns.map(&:name)
puts Widget.exists?(:accessory_id => a1) # should print "true"
["id", "accessory_id"]
/Users/davidyip/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-2.3.9/lib/active_record/association_preload.rb:320:in `send': missing attribute: accessory_id (ActiveRecord::MissingAttributeError)
from /Users/davidyip/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-2.3.9/lib/active_record/association_preload.rb:320:in `preload_belongs_to_association'
from /Users/davidyip/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-2.3.9/lib/active_record/association_preload.rb:319:in `each'
from /Users/davidyip/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-2.3.9/lib/active_record/association_preload.rb:319:in `preload_belongs_to_association'
from /Users/davidyip/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-2.3.9/lib/active_record/association_preload.rb:120:in `send'
from /Users/davidyip/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-2.3.9/lib/active_record/association_preload.rb:120:in `preload_one_association'
from /Users/davidyip/.rvm/gems/ruby-1.8.7-p302/gems/activesupport-2.3.9/lib/active_support/ordered_hash.rb:115:in `each'
from /Users/davidyip/.rvm/gems/ruby-1.8.7-p302/gems/activesupport-2.3.9/lib/active_support/ordered_hash.rb:115:in `each'
from /Users/davidyip/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-2.3.9/lib/active_record/association_preload.rb:114:in `preload_one_association'
from /Users/davidyip/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-2.3.9/lib/active_record/association_preload.rb:91:in `preload_associations'
from /Users/davidyip/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-2.3.9/lib/active_record/association_preload.rb:90:in `preload_associations'
from /Users/davidyip/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-2.3.9/lib/active_record/association_preload.rb:90:in `each'
from /Users/davidyip/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-2.3.9/lib/active_record/association_preload.rb:90:in `preload_associations'
from /Users/davidyip/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-2.3.9/lib/active_record/base.rb:1584:in `find_every'
from /Users/davidyip/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-2.3.9/lib/active_record/base.rb:1539:in `find_initial'
from /Users/davidyip/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-2.3.9/lib/active_record/base.rb:696:in `exists?'
from exists_with_scope.rb:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment