1

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

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

几周前,我实现了一种方法,该方法可以索引保管箱文件夹并保存文件名和直接链接。现在我添加了一行,将整个 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

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

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 的一些非常重要的信息?在过去的几周里,我一直在做同样的事情,从未经历过任何如此奇怪的经历。

4

1 回答 1

3

您不应该使用布尔属性,因为它通过调用方法validates_presence_of来检查属性是否存在。blank?但是,false.blank?是真的,所以当属性为假时,它永远不会让你保存记录。

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

于 2013-05-03T08:10:59.603 回答