在用户模式创建多个参与者 [英] Creating multiple participants in the User model

查看:120
本文介绍了在用户模式创建多个参与者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to set up a complex association where the User model is used two times. Once to create the leader of a discussion and then to create the participants for the discussion.

首先,我有正确的关联,以呼吁由一个用户(领导者)?

First, do I have the associations correct in order to call multiple participants (users) for a discussion led by one user (leader)?

用户

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

第二,我将如何建立操作将其分配多个用户作为参与者?

And second, how would I build the action to assign multiple users as participants?

形式:

  .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

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

我得到的错误是无法大规模指派保护属性:participant_ids

推荐答案

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

First of all you want to have has_and_belongs_to_many between User and Discussion, both when a user is a leader or participant.

首先,你应该做出决定,如果你想用户的habtm或的has_many:通过协会
<一href=\"http://guides.rubyonrails.org/association_basics.html#choosing-between-has-many-through-and-has-and-belongs-to-many\" rel=\"nofollow\">http://guides.rubyonrails.org/association_basics.html#choosing-between-has-many-through-and-has-and-belongs-to-many

First you should make the decision if you want to user habtm or has_many :through association http://guides.rubyonrails.org/association_basics.html#choosing-between-has-many-through-and-has-and-belongs-to-many

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

Then remodel your associations and the rest of your problems should solve themselves.

这篇关于在用户模式创建多个参与者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆