【发布时间】:2010-01-16 00:50:58
【问题描述】:
这可能是一件非常简单的事情,但我这辈子都做不到。出于某种原因, :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
当我在 IRB 中对数据库中已有封面的专辑执行以下操作时:
a = Album.find(1)
a.title = "New title"
a.cover_art.artist = "New Artist"
a.save
它会更新专辑记录,但不会更新 CoverArt 记录。我做错了什么?
【问题讨论】:
标签: ruby-on-rails database activerecord