在用户模式创建多个参与者(Creating multiple participants in the

2019-10-18 08:51发布

我试图建立一个复杂的关联,其中用户模型用于两次。 一旦创建一个讨论的领导者,然后创建讨论参与者。

首先,我有关联,以调用多个参与者(用户)通过一个用户(领导者)领导的讨论是否正确?

用户

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

Answer 1:

首先,你想拥有的用户和讨论之间has_and_belongs_to_many,既当用户是一个领导者或参与者。

首先,你应该做出决定,如果你想给用户的habtm或的has_many:通过关联http://guides.rubyonrails.org/association_basics.html#choosing-between-has-many-through-and-has-and-belongs-to -许多

然后改造您协会和你的问题的其余部分应自行解决。



文章来源: Creating multiple participants in the User model