《ruby on rails 教程》中示例代码执行出错

2014-10-02 11:21:19 +08:00
 strider
最近刚开始学rails,就选了一本《ruby on rails教程》来看,貌似译者也在v2ex里:)

跟着书的指导来一章一章的做下来,前面的都没有什么问题,或者有问题google一下也解决了,但到了第六章,在rails console --sandbox里,凡是执行xxx.save的语句,都会报出下面的错误,哪位大神有时间能帮我看下,谢谢!
```
(0.2ms) SAVEPOINT active_record_1
SQLite3::SQLException: near "SAVEPOINT": syntax error: SAVEPOINT active_record_1
ActiveRecord::StatementInvalid: SQLite3::SQLException: near "SAVEPOINT": syntax error: SAVEPOINT active_record_1
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `initialize'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `new'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `prepare'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:134:in `execute'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/activerecord-4.0.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:331:in `block in execute'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract_adapter.rb:442:in `block in log'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/activesupport-4.0.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract_adapter.rb:437:in `log'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/activerecord-4.0.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:331:in `execute'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/activerecord-4.0.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:355:in `create_savepoint'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/transaction.rb:192:in `initialize'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/transaction.rb:108:in `new'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/transaction.rb:108:in `begin'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:243:in `begin_transaction'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:220:in `within_new_transaction'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/activerecord-4.0.4/lib/active_record/transactions.rb:209:in `transaction'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/activerecord-4.0.4/lib/active_record/transactions.rb:327:in `with_transaction_returning_status'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/activerecord-4.0.4/lib/active_record/transactions.rb:270:in `block in save'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/activerecord-4.0.4/lib/active_record/transactions.rb:285:in `rollback_active_record_state!'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/activerecord-4.0.4/lib/active_record/transactions.rb:269:in `save'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/activerecord-4.0.4/lib/active_record/persistence.rb:34:in `create'
from (irb):1
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/railties-4.0.4/lib/rails/commands/console.rb:90:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/railties-4.0.4/lib/rails/commands/console.rb:9:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p481@railstutorial_rails_4_0/gems/railties-4.0.4/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
```
3246 次点击
所在节点    问与答
7 条回复
tiseheaini
2014-10-02 11:56:25 +08:00
把你在 rails console 中执行的代码也贴出来看一下
strider
2014-10-02 12:04:02 +08:00
2.0.0-p481 :001 > user = User.new(name: "Michael Hartl", email: "mhartl@example.com")
=> #<User id: nil, name: "Michael Hartl", email: "mhartl@example.com", created_at: nil, updated_at: nil>
2.0.0-p481 :002 > user.save
(0.2ms) SAVEPOINT active_record_1
SQLite3::SQLException: near "SAVEPOINT": syntax error: SAVEPOINT active_record_1
ActiveRecord::StatementInvalid: SQLite3::SQLException: near "SAVEPOINT": syntax error: SAVEPOINT active_record_1
(略)
whywhywhy
2014-10-02 12:47:21 +08:00
书上的代码有错误那是正常得不得不了,以前傻傻的不知道,照抄下来执行不了,也不懂怎么怎么查哪的问题,就傻逼了
bullfrog
2014-10-02 12:53:47 +08:00
rails用法没错,SQLite的问题, 解决方法google一搜就有
http://shynnergy.com/2012/07/savepoint-exception-with-rails-3-1-on-jenkins/
Andor_Chen
2014-10-02 14:20:38 +08:00
@strider 我是译者,感谢阅读这本书。

关于阅读过程中遇到的问题,我的观点是:

1. 尽信书不如无书;
2. 每个人的环境不一样,在作者/译者的环境中不出错,不代表在其他环境中一定能正常运行;
3. 遇到问题是好事。因为你能从中学习如何解决问题,如何向他人寻求帮助。而且你比其他人多学了一点儿。

你说的问题应该是环境导致的,可以按着 @bullfrog 提供的思路去解决。

如果解决不了,可以到 Ruby China ( https://ruby-china.org/topics ) 寻求帮助。欢迎随时 @ 我。
jacob
2014-10-02 23:31:58 +08:00
我最近也在看这本书,如果你跟我一样用的rails和gem都是最新版的话,那么恭喜你基本每章都有错误,版本更新的问题。你这个错误我没碰到过,你试试先迁移下数据库,在看看你模型文件和控制器文件跟书上一样不,这部分没错。另外关掉其他终端,在一个终端操作试试。我现在看到更新用户那章,可以交流下。
strider
2014-10-03 10:59:57 +08:00
感谢楼上各位回复者~
今天我再看下,如果解决了的话我在帖子里也更新一下

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/136777

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX