Rails 3 - 创建相关模型时自动编号?

时间:2012-04-05 18:21:39

标签: ruby-on-rails ruby-on-rails-3 activerecord ruby-on-rails-3.1 nested-forms

我有类似以下的模型 -

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中做些什么?

0 个答案:

没有答案