這讓我瘋狂!這段代碼過去工作正常,但幾個星期前它停止工作,我無法解決原因。基本上一場比賽有很多補丁。在我PatchesController出現的錯誤,但其在軌控制檯這樣的重複性:ActiveRecord爲簡單的has_many關聯提供'沒有這樣的列'SQL錯誤
first_game = Game.find(:first)
first_game.patches
只要我使用補丁的方法,我得到這樣的:
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>'
現在,SQL應該說真的'WHERE patches.game_id = 1',除非我生氣。我不知道爲什麼它會生成該SQL!
這裏的模型/ game.rb:
class Game < ActiveRecord::Base
has_many :patches
end
這裏的模型/ patches.rb:
class Patch < ActiveRecord::Base
belongs_to :game
end
而且補丁表中有 'game_id' 在餐桌上,和3項,全部爲第一場比賽。如果我得到其中一個修補程序並轉到my_patch.game,它會返回它所屬的Game對象,而不會出現任何問題。任何幫助將不勝感激!
如果您手動設置關聯關鍵字,它是否工作?瞭解你不應該設置它,我只是好奇,如果我們可以找出錯誤的地方。 – jasonpgignac 2010-07-19 13:50:35
Patch.find(:all,:conditions => {:game_id => 1})工作正常! first_game.patches.find(:all,:conditions => {:game_id => 1})給出相同的錯誤,並生成此SQL: SELECT * FROM「patches」WHERE(「patches」.game_true = 1 AND(「patches」,「game_id」= 1)) – TheAdmiral 2010-07-19 14:46:58