在单独的表单字段中包含连接模型中的属性

时间:2017-02-08 14:02:26

标签: ruby-on-rails join has-many-through

我有一个关联设置,其中Track可以属于多个ReleasesReleaseTracks

class Release < ApplicationRecord
  has_many :release_tracks, dependent: :destroy
  has_many :tracks, through: :release_tracks
end

class ReleaseTrack < ApplicationRecord
  # disc, side, number
  belongs_to :release
  belongs_to :track

  belongs_to :album
end

class Track < ApplicationRecord
  has_many :release_tracks, dependent: :destroy
  has_many :releases, through: :release_tracks

  validates_uniqueness_of :name, scope: [:release_id, :album_id]
end

如您所见,ReleaseTrack有三个属性disc, side, number,可以正确识别任何特定版本的曲目的正确位置。我想知道在发布表单中创建新曲目的例子,我怎么能保存这三个属性呢?

0 个答案:

没有答案