这让我疯了!这段代码过去工作正常,但几周前它停止了工作,我无法解决原因。基本上游戏有很多补丁。错误发生在我的PatchesController中,但它在rails控制台中可重现,如下所示:
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
这是models / patches.rb:
class Patch < ActiveRecord::Base
belongs_to :game
end
补丁表在表格中有'game_id',还有3个条目,全部用于第一个游戏。如果我得到其中一个补丁并转到my_patch.game,它将返回它所属的Game对象,没有任何问题。任何帮助将不胜感激!
答案 0 :(得分:3)
我能够找出问题所在。它在我的迁移文件中。我在迁移文件中使用了引用帮助器。
def up
create_table :reviews do |t|
t.integer :potatoes
t.text :comments
t.references :moviegoer
t.references :movie
end
我拼错了父类的模型名称。更正了名称,然后删除了我的数据库并重新创建了它 rake db:drop rake db:migrate
答案 1 :(得分:0)
看起来您出于某种原因正在更改主键的名称。确保您的Game类没有类似的内容:
class Game < ActiveRecord::Base
# Form #1
self.primary_key = true
# Form #2
set_primary_key true
end
这可以用来将主键列从'id'重命名为任意的,在你的情况下看起来是'true'。
答案 2 :(得分:0)
SQLException: no such column: patches.game_true: SELECT * FROM "patches" WHERE ("patches".game_true = 1)
这应该改变你需要去的地方。