我有类似于以下的模型 -
Table: series
- id: integer
- volumes: integer
- title: string
class Series < ActiveRecord::Base
has_many :books, :dependent => :destroy
accepts_nested_attributes_for :books, :reject_if =>
proc { |attributes| attributes[:title].blank? }
validates_associated :books
end
Table: books
- id: integer
- series_id: integer
- title: string
- volume: integer
class Book < ActiveRecord::Base
belongs_to :series
validates :title, :volume, :presence => true
validates :title, :uniqueness =>
{ :scope => :series_id,
:message => 'Book titles must be unique within a series.'}
end
我正在使用nested_form gem 构建一个统一的表单,通过同一个表单输入系列信息和每本书的信息。
有没有办法在每本书添加到系列时自动填充每本书的“音量”属性?
我是否需要修改nested_form 生成的JavaScript 来完成此操作,或者我可以在ActiveRecord 中做些什么?