V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
strider
V2EX  ›  问与答

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

  •  
  •   strider · 2014-10-02 11:21:19 +08:00 · 3215 次点击
    这是一个创建于 3703 天前的主题,其中的信息可能已经有所发展或是发生改变。
    最近刚开始学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'
    ```
    7 条回复    2014-10-03 10:59:57 +08:00
    tiseheaini
        1
    tiseheaini  
       2014-10-02 11:56:25 +08:00   ❤️ 1
    把你在 rails console 中执行的代码也贴出来看一下
    strider
        2
    strider  
    OP
       2014-10-02 12:04:02 +08:00
    2.0.0-p481 :001 > user = User.new(name: "Michael Hartl", email: "[email protected]")
    => #<User id: nil, name: "Michael Hartl", email: "[email protected]", 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
        3
    whywhywhy  
       2014-10-02 12:47:21 +08:00   ❤️ 1
    书上的代码有错误那是正常得不得不了,以前傻傻的不知道,照抄下来执行不了,也不懂怎么怎么查哪的问题,就傻逼了
    bullfrog
        4
    bullfrog  
       2014-10-02 12:53:47 +08:00   ❤️ 1
    rails用法没错,SQLite的问题, 解决方法google一搜就有
    http://shynnergy.com/2012/07/savepoint-exception-with-rails-3-1-on-jenkins/
    Andor_Chen
        5
    Andor_Chen  
       2014-10-02 14:20:38 +08:00   ❤️ 2
    @strider 我是译者,感谢阅读这本书。

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

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

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

    如果解决不了,可以到 Ruby China ( https://ruby-china.org/topics ) 寻求帮助。欢迎随时 @ 我。
    jacob
        6
    jacob  
       2014-10-02 23:31:58 +08:00 via iPhone   ❤️ 1
    我最近也在看这本书,如果你跟我一样用的rails和gem都是最新版的话,那么恭喜你基本每章都有错误,版本更新的问题。你这个错误我没碰到过,你试试先迁移下数据库,在看看你模型文件和控制器文件跟书上一样不,这部分没错。另外关掉其他终端,在一个终端操作试试。我现在看到更新用户那章,可以交流下。
    strider
        7
    strider  
    OP
       2014-10-03 10:59:57 +08:00
    感谢楼上各位回复者~
    今天我再看下,如果解决了的话我在帖子里也更新一下
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1111 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 22:53 · PVG 06:53 · LAX 14:53 · JFK 17:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.