views:

115

answers:

2

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!

A: 

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'.

tadman
Sadly the entire code of the Game class is there in the original post. :( I did a search through my project files, and the only time 'primary_key' and 'set_primary_key' show up is in the frozen rails gems.
TheAdmiral
I created a scratch Rails 2.3.5 project and generated a Game and Patch class like you have here, but it works fine. What back-end are you using? This could be a problem with the DB. Can you replicate this with the sqlite3 engine, for example?
tadman
I'm using rails 2.3.8 and sqlite3 at the moment… I did notice an update to the ruby-sqlite3 gem go past a little while ago, so that might have broken it. I'll have a go with MySQL and see what happens.
TheAdmiral
Getting exactly the same error using MySQL: Unknown column 'patches.game_true' in 'where clause': SELECT * FROM `patches` WHERE (`patches`.game_true = 1)
TheAdmiral
If it shows up under MySQL too, maybe there's something wrong with your installation. I'm not sure if you can try and run this on another system with a fresh set of gems, or if you can reproduce it in a scratch project on the same machine. I made a quick Rails app with the same models you describe and it works, so it's not a case of reserved word conflict as can be the case sometimes.
tadman
Same here, these work fine for me, so the problem might be somewhere else in your codebase. Try temporarily removing your frozen rails, and see if it works, and if not, strip out any plugins, lib files, helpers, etc (in approximately that order) to see if the problem dissapears. Either something is altering the behaviour of ActiveRecord, or something is altering the behaviour of your class.
jasonpgignac
A: 

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.

Taryn East