我试图建立一个复杂的关联,其中用户模型用于两次。 一旦创建一个讨论的领导者,然后创建讨论参与者。
首先,我有关联,以调用多个参与者(用户)通过一个用户(领导者)领导的讨论是否正确?
用户
has_many :discussions, foreign_key: :leader_id
belongs_to :received_discussions, class_name: 'Discussion'
attr_accessible :participant_id
讨论
belongs_to :leader, class_name: 'User'
has_many :participants, class_name: 'User', foreign_key: :participant_id
第二,我将如何建立分配多个用户作为参与者的行动?
形成:
.offset2.span7.mtop20
= simple_form_for @discussion, html: { multipart: true } do |f|
%fieldset.well
.mtop10
= f.input :participant_ids, label: 'Send to: (select from members you follow)', as: :select, collection: participants_for_discussion, input_html: { class: 'followed-ids-select', multiple: true }
= f.input :title
= f.input :description
.form-actions
= f.submit 'Send', name: 'send_now', class: 'btn btn-primary btn-large pull-right mleft10'
= f.submit 'Save and View Draft', name: 'save_draft', class: 'btn btn-large pull-right'
discussion_controller
def create
#not sure how to assign the participants in the action
@discussion = current_user.discussions.build(params[:discussion])
if @discussion.save
redirect_to @discussion, notice: draft_or_sent_notice
else
render :new
end
end
我得到的错误是Can't mass-assign protected attributes: participant_ids