Reputation: 31
This is driving me insane! This code used to work fine, but as of a few weeks ago it stopped working, and I can't work out why. Basically a Game has many Patches. The error occurs in my PatchesController, but its reproducible in the rails console like this:
first_game = Game.find(:first)
first_game.patches
As soon as I use the patches method, I get this:
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: patches.game_true: SELECT * FROM "patches" WHERE ("patches".game_true = 1)
from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:221:in `rescue in log'
from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:204:in `log'
from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:172:in `block in execute'
from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:417:in `catch_schema_changes'
from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:172:in `execute'
from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:320:in `select'
from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all'
from /project_root/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all_with_query_cache'
from /project_root/vendor/rails/activerecord/lib/active_record/base.rb:664:in `find_by_sql'
from /project_root/vendor/rails/activerecord/lib/active_record/base.rb:1578:in `find_every'
from /project_root/vendor/rails/activerecord/lib/active_record/base.rb:618:in `find'
from /project_root/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:60:in `find'
from /project_root/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:400:in `find_target'
from /project_root/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:354:in `load_target'
from /project_root/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:140:in `inspect'
from /usr/local/bin/irb:12:in `<main>'
Now that SQL should really say 'WHERE patches.game_id = 1', unless I'm going mad. I have no idea why it's generating that SQL!
Here's models/game.rb:
class Game < ActiveRecord::Base
has_many :patches
end
Here's models/patches.rb:
class Patch < ActiveRecord::Base
belongs_to :game
end
And the patches table has 'game_id' in the table, and 3 entries, all for the first game. If I get one of the Patches and go my_patch.game, it returns the Game object it belongs to, with no problems. Any help would be greatly appreciated!
Upvotes: 3
Views: 3037
Reputation: 1242
I was able to figure out the problem. It was in my migration file. I was using the references helper in my migration file.
def up
create_table :reviews do |t|
t.integer :potatoes
t.text :comments
t.references :moviegoer
t.references :movie
end
I had misspelled the model name of the parent class. Corrected the name, then dropped my database and recreated it rake db:drop rake db:migrate
Upvotes: 3
Reputation: 27747
ummm silly question, but doesn't that backtrace claim that the missing column is called "game_true" (on the patches table) ? I don't think it's claiming that there's no column called "patches".
SQLException: no such column: patches.game_true: SELECT * FROM "patches" WHERE ("patches".game_true = 1)
That should change where you need to go look.
Upvotes: 0
Reputation: 211590
It looks like you're changing the name of the primary key for some reason. Make sure your Game class doesn't have anything like:
class Game < ActiveRecord::Base
# Form #1
self.primary_key = true
# Form #2
set_primary_key true
end
This can be used to rename the primary key column from 'id' to something arbitrary, which in your case appears to be 'true'.
Upvotes: 0