在微小的模型操作之后,Rails不会保存到数据库

时间:2013-05-02 15:37:47

标签: ruby-on-rails ruby

我正在编写我的第一个Rails应用程序,但无法解决这个恼人的错误。

我想做什么:该应用已连接到用户的保管箱帐户。用户可以指定应用程序随后查找音频文件的路径。然后将这些音频文件下载,元数据,路径等保存到数据库中的Song模型中,该模型属于相应的用户。

我实现了一个方法,该方法会对Dropbox文件夹编制索引,并在几周前保存文件名和直接链接。现在我添加了一行,将整个Dropbox路径保存到数据库中,突然间什么都不再保存了。

这就是我的歌曲模型:

require "dropbox_helper"
require "open-uri"

class Song < ActiveRecord::Base
  belongs_to :user
  attr_accessible :artist, :expiration, :is_cached, :length, :dropbox_link, :local_path, :title, :dropbox_path

  validates_presence_of :title, :is_cached, :dropbox_link, :dropbox_path
  #validates :dropbox_path, :uniqueness => true

  def self.fetch_basic_songdata(user)
    logger.info "++++DEBUG: Song#fetch_basic_songdata started"
    dbh = DropboxHelper.new(user.dropbox_access_key, user.dropbox_access_secret, user.dropbox_folder)
    logger.info "++++DEBUG: DropboxHelper initialized"
    songs_metadata = dbh.generate_audio_index
    logger.info "++++DEBUG: Metadata generated"
    #logger.debug songs_metadata.inspect
    songs_metadata.each do |smd|
      logger.info "Preparing song #{smd['path']} for saving"
      songdata = dbh.song_data(smd)
      user.songs.create do |s|
        s.title = songdata['filename']
        s.dropbox_path = songdata['path']
        s.dropbox_link = songdata['link']
        s.is_cached = false
        logger.info "Song #{s[:title]} generated"
        logger.info "Song #{s[:title]} saved to the data base"
      end
    end
  end

end

现在,当我在rails控制台中调用Song.fetch_basic_songdata(User.find(1))时,会打印以下内容:

irb(main):002:0> Song.fetch_basic_songdata(User.find(1))
  User Load (59.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
Song#fetch_basic_songdata started
Preparing song /audiomixer/01 - Paradise.mp3 for saving
   (0.1ms)  begin transaction
Song 01 - Paradise.mp3 generated
Song 01 - Paradise.mp3 saved to the data base
   (0.1ms)  commit transaction
Preparing song /audiomixer/02 - Weirdo.mp3 for saving
   (0.1ms)  begin transaction
Song 02 - Weirdo.mp3 generated
Song 02 - Weirdo.mp3 saved to the data base
   (0.2ms)  commit transaction
Preparing song /audiomixer/03 - As It Is When It Was.mp3 for saving
   (0.1ms)  begin transaction
Song 03 - As It Is When It Was.mp3 generated
Song 03 - As It Is When It Was.mp3 saved to the data base
   (0.1ms)  commit transaction
Preparing song /audiomixer/04 - Broken Promise.mp3 for saving
   (0.2ms)  begin transaction
Song 04 - Broken Promise.mp3 generated
Song 04 - Broken Promise.mp3 saved to the data base
   (0.1ms)  commit transaction
Preparing song /audiomixer/05 - Way Of Life.mp3 for saving
   (0.1ms)  begin transaction
Song 05 - Way Of Life.mp3 generated
Song 05 - Way Of Life.mp3 saved to the data base
   (0.1ms)  commit transaction
Preparing song /audiomixer/06 - Bizarre Love Triangle.mp3 for saving
   (0.1ms)  begin transaction
Song 06 - Bizarre Love Triangle.mp3 generated
Song 06 - Bizarre Love Triangle.mp3 saved to the data base
   (0.1ms)  commit transaction
Preparing song /audiomixer/07 - All Day Long.mp3 for saving
   (0.1ms)  begin transaction
Song 07 - All Day Long.mp3 generated
Song 07 - All Day Long.mp3 saved to the data base
   (0.1ms)  commit transaction
Preparing song /audiomixer/08 - Angel Dust.mp3 for saving
   (0.1ms)  begin transaction
Song 08 - Angel Dust.mp3 generated
Song 08 - Angel Dust.mp3 saved to the data base
   (0.1ms)  commit transaction
Preparing song /audiomixer/09 - Every Little Counts.mp3 for saving
   (0.1ms)  begin transaction
Song 09 - Every Little Counts.mp3 generated
Song 09 - Every Little Counts.mp3 saved to the data base
   (0.1ms)  commit transaction
Preparing song /audiomixer/10 - State Of The Nation.mp3 for saving
   (0.1ms)  begin transaction
Song 10 - State Of The Nation.mp3 generated
Song 10 - State Of The Nation.mp3 saved to the data base
   (0.0ms)  commit transaction
=> [#<Dropbox::API::File bytes=3710598 client_mtime="Fri, 23 Jul 2010 00:09:30 +0000" icon="page_white_sound" is_dir=false mime_type="audio/mpeg" modified="Mon, 22 Apr 2013 12:27:54 +0000" path="/audiomixer/01 - Paradise.mp3" rev="4b6ad8fc0010df0f" revision=1265293564 root="dropbox" size="3.5 MB" thumb_exists=false>, #<Dropbox::API::File bytes=3731496 client_mtime="Fri, 23 Jul 2010 00:09:30 +0000" icon="page_white_sound" is_dir=false mime_type="audio/mpeg" modified="Mon, 22 Apr 2013 12:27:54 +0000" path="/audiomixer/02 - Weirdo.mp3" rev="4b6ad8fd0010df0f" revision=1265293565 root="dropbox" size="3.6 MB" thumb_exists=false>, #<Dropbox::API::File bytes=3620319 client_mtime="Sun, 10 Jan 2010 19:50:28 +0000" icon="page_white_sound" is_dir=false mime_type="audio/mpeg" modified="Mon, 22 Apr 2013 12:27:54 +0000" path="/audiomixer/03 - As It Is When It Was.mp3" rev="4b6ad8fa0010df0f" revision=1265293562 root="dropbox" size="3.5 MB" thumb_exists=false>, #<Dropbox::API::File bytes=3645815 client_mtime="Sun, 10 Jan 2010 19:50:28 +0000" icon="page_white_sound" is_dir=false mime_type="audio/mpeg" modified="Mon, 22 Apr 2013 12:27:54 +0000" path="/audiomixer/04 - Broken Promise.mp3" rev="4b6ad8fb0010df0f" revision=1265293563 root="dropbox" size="3.5 MB" thumb_exists=false>, #<Dropbox::API::File bytes=3954295 client_mtime="Sat, 20 Apr 2013 07:09:30 +0000" icon="page_white_sound" is_dir=false mime_type="audio/mpeg" modified="Mon, 22 Apr 2013 12:27:54 +0000" path="/audiomixer/05 - Way Of Life.mp3" rev="4b6ad8fe0010df0f" revision=1265293566 root="dropbox" size="3.8 MB" thumb_exists=false>, #<Dropbox::API::File bytes=4199193 client_mtime="Sun, 10 Jan 2010 19:50:28 +0000" icon="page_white_sound" is_dir=false mime_type="audio/mpeg" modified="Mon, 22 Apr 2013 12:27:54 +0000" path="/audiomixer/06 - Bizarre Love Triangle.mp3" rev="4b6ad8ff0010df0f" revision=1265293567 root="dropbox" size="4 MB" thumb_exists=false>, #<Dropbox::API::File bytes=5002092 client_mtime="Sun, 10 Jan 2010 19:50:28 +0000" icon="page_white_sound" is_dir=false mime_type="audio/mpeg" modified="Mon, 22 Apr 2013 12:27:54 +0000" path="/audiomixer/07 - All Day Long.mp3" rev="4b6ad9010010df0f" revision=1265293569 root="dropbox" size="4.8 MB" thumb_exists=false>, #<Dropbox::API::File bytes=3595686 client_mtime="Sat, 20 Apr 2013 07:09:31 +0000" icon="page_white_sound" is_dir=false mime_type="audio/mpeg" modified="Mon, 22 Apr 2013 12:27:54 +0000" path="/audiomixer/08 - Angel Dust.mp3" rev="4b6ad8f90010df0f" revision=1265293561 root="dropbox" size="3.4 MB" thumb_exists=false>, #<Dropbox::API::File bytes=4307888 client_mtime="Sat, 20 Apr 2013 07:09:32 +0000" icon="page_white_sound" is_dir=false mime_type="audio/mpeg" modified="Mon, 22 Apr 2013 12:27:54 +0000" path="/audiomixer/09 - Every Little Counts.mp3" rev="4b6ad9000010df0f" revision=1265293568 root="dropbox" size="4.1 MB" thumb_exists=false>, #<Dropbox::API::File bytes=6313648 client_mtime="Sun, 10 Jan 2010 19:50:28 +0000" icon="page_white_sound" is_dir=false mime_type="audio/mpeg" modified="Mon, 22 Apr 2013 12:27:54 +0000" path="/audiomixer/10 - State Of The Nation.mp3" rev="4b6ad9020010df0f" revision=1265293570 root="dropbox" size="6 MB" thumb_exists=false>]

检查是否保存了任何内容:

irb(main):003:0> Song.all
  Song Load (0.3ms)  SELECT "songs".* FROM "songs" 
=> []

我还注意到以logger.info "++++DEBUG:[...]"开头的行根本没有打印。在我插入之前,下面的logger.info行没有打印出来。我真的没有这种行为,你们中的任何人都可以帮忙吗?

PS:我希望提供的信息足够。由于应用程序的行为对我来说完全是随机的,因此我很难理解对问题至关重要的是什么。

编辑:感谢您的评论!按照建议,我尝试使用create!。这是它告诉我的:

irb(main):002:0> Song.fetch_basic_songdata(User.find(1))
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
++++DEBUG: Song#fetch_basic_songdata started
++++DEBUG: DropboxHelper initialized
++++DEBUG: Metadata generated
Preparing song /audiomixer/01 - Paradise.mp3 for saving
   (0.1ms)  begin transaction
Song 01 - Paradise.mp3 generated
Song 01 - Paradise.mp3 saved to the data base
   (0.2ms)  rollback transaction
ActiveRecord::RecordInvalid: Validation failed: Is cached can't be blank
    from /var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/validations.rb:56:in `save!'
    from /var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/attribute_methods/dirty.rb:33:in `save!'
    from /var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/transactions.rb:264:in `block in save!'
    from /var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
    from /var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
    from /var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/transactions.rb:208:in `transaction'
    from /var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
    from /var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/transactions.rb:264:in `save!'
    from /var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/has_many_association.rb:14:in `insert_record'
    from /var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:436:in `block (2 levels) in create_record'
    from /var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:344:in `add_to_target'
    from /var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:434:in `block in create_record'
    from /var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:149:in `block in transaction'
    from /var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
    from /var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/transactions.rb:208:in `transaction'
    from /var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:148:in `transaction'
    from /var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:433:in `create_record'
    from /var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:123:in `create!'
    from /var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/collection_proxy.rb:46:in `create!'
    from /home/sloth/audiomixer/app/models/song.rb:21:in `block in fetch_basic_songdata'
    from /home/sloth/audiomixer/app/models/song.rb:18:in `each'
    from /home/sloth/audiomixer/app/models/song.rb:18:in `fetch_basic_songdata'
    from (irb):2
    from /var/lib/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
    from /var/lib/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
    from /var/lib/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `

因此,如果我正确解释了这个问题,问题是is_cached变量似乎是空白的(空?无?)。我认为这是由于在s.is_cached = false分配之前发生错误。任何想法如何找到它?

此外,我刚评论了一些logger.info行并添加了新行。重新启动服务器后,我再次尝试打印了我评论的行,但没有新的。我是否缺少使用旧文件的Rails的一些非常重要的信息?在过去的几周里,我一直在做同样的事情,从未经历过任何这种奇怪的经历。

1 个答案:

答案 0 :(得分:3)

您不应将validates_presence_of与布尔属性一起使用,因为它通过调用blank?方法检查该属性是否存在。但是,false.blank?为真,因此当属性为false时,它永远不会让您保存记录。

您可能还需要注意,rails控制台不会自动选择您对代码所做的更改。您需要调用reload!或重新启动控制台。使用reload!时,请注意在重新加载调用之前没有创建对象实例!闲逛 - 他们可能指的是上一版的课程。