我有 模特
class Group < ApplicationRecord
has_many :group_artists
has_many :singers, -> { where role: "Singer" }, class_name: "GroupArtist"
has_many :guitarists, -> { where role: "Guitarist" }, class_name: "GroupArtist"
end
class GroupArtist < ApplicationRecord
belongs_to :group
belongs_to :artist
end
class Artist < ApplicationRecord
has_many :group_artists
has_many :groups, through: :group_artists
end
group_artists表包含这些列
class CreateGroupArtists < ActiveRecord::Migration[5.1]
def change
create_table :group_artists, id: false do |t|
t.references :group, foreign_key: true, null: false
t.references :artist, foreign_key: true, null: false
t.string :role
t.string :acting
t.timestamps
end
end
end
控制器
class GroupsController < ApplicationController
def new
@group = Group.new
@singers = @group.singers.build
@guitarists = @group.guitarists.build
@artists = Artist.all // for a selection
end
def create
@group = Group.new(allowed_params)
@group.save
end
private
def allowed_params
params.require(:group).permit(:name, :singers, :guitarists, group_artists_attributes: [:group_id, :artist_id, :role, :acting])
end
end
视图/组/ _form.html.erb
<%= form_for @group do |f| %>
<%= f.label "Singers" %>
<%= f.fields_for :singers do |singer| %>
<%= singer.select(:artist_id, @artists.collect { |a| [a.name, a.id.to_i] }, { include_blank: true }) %>
<% end %>
<%= f.label "Guitarists" %>
<%= f.fields_for :guitarists do |guitarist| %>
<%= guitarist.select(:artist_id, @artists.collect { |a| [a.name, a.id.to_i] }, { include_blank: true }) %>
<% end %>
<%= f.submit "Submit" %>
<% end %>
它可以创建组,但不会在GroupArtist中创建关系。 我知道控制器部分缺少某些东西。我应该在“.build”之后添加一些东西(角色:“歌手”),但它也没有做任何事情。
Ruby -v 2.4.1
Rails -v 5.1.3
答案 0 :(得分:2)
由于您使用的group_artists
不仅仅是一个简单的连接表,因此您需要使用nested attributes来创建包含元数据的行:
class Group < ApplicationRecord
has_many :group_artists
has_many :singers, -> { where role: "Singer" }, class_name: "GroupArtist"
has_many :guitarists, -> { where role: "Guitarist" }, class_name: "GroupArtist"
accepts_nested_attributes_for :group_artists,
reject_if: ->{|a| a[:artist_id].blank? || a[:role].blank?}
end
使用不同关联来创建基于乐队成员角色的嵌套记录的结构也不是真正可扩展的 - 对于每个可能的角色,类/表格都会膨胀。
相反,您可能想要使用两个选择:
<%= form_for @group do |f| %>
<fields_for :group_artists do |ga| %>
<div class="field">
<%= f.label :artist_id, "Artist" %>
<%= f.collection_select :artist_id, Artist.all, :id, :name %>
<%= f.label :role %>
<%= f.select :role, %w[ Singer Guitarist ] %>
</div>
<% end %>
<%= f.submit "Submit" %>
<% end %>
此外,您无法使用#create
方法保存记录。
def create
@group = Group.new(allowed_params)
if @group.save
# ...
else
render :new
end
end
答案 1 :(得分:1)
您应该在控制器动作创建
中添加它def create
@group = Group.new(allowed_params)
@group.save
end
Group.new不会在数据库中保留模型,但会在保存之后保存。
答案 2 :(得分:0)
这些是我需要的增加和改变才能使其发挥作用。
class Group < ApplicationRecord
has_many :group_artists
has_many :singers, -> { where role: "Singer" }, class_name: "GroupArtist"
has_many :guitarists, -> { where role: "Guitarist" }, class_name: "GroupArtist"
accepts_nested_attributes_for :group_artists,
reject_if: proc { |a| a[:artist_id].blank? || a[:role].blank? },
allow_destroy: true
end
class GroupsController < ApplicationController
def new
@group = Group.new
@group.group_artists.build
@artists = Artist.all // for a selection
end
def create
@group = Group.new(allowed_params)
@group.save
end
end
基团/ _form.html.erb
<%= form_for @group do |f| %>
<div class="singers">
<%= f.label :singers %>
<%= f.fields_for :group_artists do |ga| %>
<%= ga.collection_select :artist_id, Artist.all, :id, :name %>
<%= ga.hidden_field :role, value: 'Singer' %>
<% end %>
</div>
<div class="guitarists">
<%= f.label :guitarists %>
<%= f.fields_for :group_artists do |ga| %>
<%= ga.collection_select :artist_id, Artist.all, :id, :name %>
<%= ga.hidden_field :role, value: 'Guitarist' %>
<% end %>
</div>
<%= f.submit "Submit" %>
<% end %>
如果他的名字在歌手或吉他手中被选中,则你不必指定艺术家的角色。 就是这样。感谢max,指导我走向正确的方向。