我有一个模型
class CqmExternalId < ActiveRecord::Base
set_table_name :cqm_external_id
has_many :ipp_measure_ids, :class_name => "CqmMeasureId", :foreign_key => "cqm_external_id", :conditions => [" name = 'IPP'"]
has_many :numer_measure_ids, :class_name => "CqmMeasureId", :foreign_key => "cqm_external_id", :conditions => [" name = 'NUMER'"]
has_many :denom_measure_ids, :class_name => "CqmMeasureId", :foreign_key => "cqm_external_id", :conditions => [" name = 'DENOM'"]
has_many :denex_measure_ids, :class_name => "CqmMeasureId", :foreign_key => "cqm_external_id", :conditions => [" name = 'DENEX'"]
end
class CqmMeasureId < ActiveRecord::Base
set_table_name :cqm_measure_id
belongs_to :cqm_external, :class_name => "cqm_external", :foreign_key => "cqm_external_id"
end
如何进行ipp_measure_ids
关联以在其上插入默认值create
c = CqmExternalId.first
c.ipp_measure_ids.create!(:key=>"ABC-1234") => #<CqmMeasureId id: 1, name:nil, key:"ABC-1234>"
# I want the name attribute to be initialized automatically as "IPP"