Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Can';t获取Rails:autosave选项以在模型上工作_Ruby On Rails_Database_Activerecord - Fatal编程技术网

Ruby on rails Can';t获取Rails:autosave选项以在模型上工作

Ruby on rails Can';t获取Rails:autosave选项以在模型上工作,ruby-on-rails,database,activerecord,Ruby On Rails,Database,Activerecord,这可能是一件非常简单的事情,但我一辈子都不能让它工作。出于某种原因,:autosave实际上并不是自动保存底层模型 这是我的模式: create_table :albums do |t| t.string :title t.text :review t.timestamps end create_table :songs do |t| t.integer :album_id t.string :name t.integer :length end c

这可能是一件非常简单的事情,但我一辈子都不能让它工作。出于某种原因,:autosave实际上并不是自动保存底层模型

这是我的模式:

    create_table :albums do |t| 
  t.string :title
  t.text :review

  t.timestamps
end 

create_table :songs do |t| 
  t.integer :album_id
  t.string :name
  t.integer :length
end 

create_table :cover_arts do |t| 
  t.integer :album_id
  t.integer :artist
end 
以下是我的模型:

class Album < ActiveRecord::Base
  has_many :songs, :autosave => true
  has_one :cover_art, :autosave => true
end

class CoverArt < ActiveRecord::Base
  belongs_to :album
end

class Song < ActiveRecord::Base
  belongs_to :album
end
它更新相册记录,但不更新CoverArt记录。我做错了什么?

根据,您必须保存父记录,而不仅仅是设置一个新值,子记录也要保存

post = Post.find(1)
post.title # => "The current global position of migrating ducks"
post.author.name # => "alloy"

post.title = "On the migration of ducks"
post.author.name = "Eloy Duran"

post.save
post.reload
post.title # => "On the migration of ducks"
post.author.name # => "Eloy Duran"

当这种情况发生在我身上时,我发现betternestedset插件正在覆盖更新方法,而没有使用alias_method_chain或其他任何东西来维护现有的调用链。我替换了betternestedset的更新重写,并替换了一个简单的attr_readonly调用(类似于该插件中现有的attr_protected调用)。也许这会对某些人有所帮助。

对不起,我在上面的消息中没有提到这一点——我实际上也在这么做。所以这也不是问题所在-我编辑了上面的帖子来澄清。也许你可以将控制台中得到的输出包括在内?嗨,弗洛伊德,我在控制台中得到的唯一输出是保存的返回值“true”。查看development.log文件,这是因为album对象已成功保存。然而,封面艺术品并非如此。它根本不会出现在日志文件中。因此,不幸的是,控制台或日志文件中都没有错误消息来指示可能存在的问题。我的意思是,您确认值的整个过程没有正确设置,使用重新加载等。不幸的是,这里没有太多明显的错误,所以很难回答你的问题。。。
post = Post.find(1)
post.title # => "The current global position of migrating ducks"
post.author.name # => "alloy"

post.title = "On the migration of ducks"
post.author.name = "Eloy Duran"

post.save
post.reload
post.title # => "On the migration of ducks"
post.author.name # => "Eloy Duran"