我有两个模型,Artist
它们User
通过第三个模型连接,ArtistMembership
.
从编辑/新艺术家表单中,我希望能够在该艺术家的现有 ArtistMembership 关系中编辑任何用户的角色,删除 ArtistMemberships,并添加新的 AtistMembership 关系,其中包括用户和:role。
这是我的艺术家模型:
class Artist < ActiveRecord::Base
has_many :artist_memberships, foreign_key: "artist_id", dependent: :destroy
attr_accessible :bio, :created_at, :email, :location, :name, :updated_at, :website, :pic
accepts_nested_attributes_for :artist_memberships, :allow_destroy => :true
...
end
这是我的用户模型:
class User < ActiveRecord::Base
...
has_many :artist_memberships, foreign_key: "user_id"
...
end
这是我的 ArtistMembership 模型:
class ArtistMembership < ActiveRecord::Base
belongs_to :artist, class_name: "Artist"
belongs_to :user, class_name: "User"
attr_accessible :artist_id, :created_at, :role, :user_id
end
如果我也有一个 _form.hml.erb,用于编辑开始的艺术家:
<%= form_for @artist do |artist_form| %>
<div class="field">
<%= artist_form.label :name %>
<%= artist_form.text_field :name %>
</div>
..
<div class="actions">
<%= artist_form.submit %>
</div>
<% end %>
如何为上述功能创建相关的 ArtistMembership 表单?