【发布时间】:2016-03-11 08:25:43
【问题描述】:
我正在尝试制作一个应用程序供学生发布项目。当他们创建帖子时,他们可以添加他们的队友。每个学生都有一个用户 ID,我希望创建项目的学生能够从与他们的队友相同的组织中选择其他 ID。模型关联是:
用户
has_and belongs_to_many :projects
项目
has_and belongs_to_many :users
我有一个项目模型,其中:
:user_id (integer)
:team_mates (integer)
在我的项目表单中,我希望学生(创建项目,从属于同一组织的学生列表中选择其他 ID)作为队友。我的第一个问题是队友属性是否应该是整数(因为可能有多个队友,这种情况下,这个属性可以保存一个数组吗?
我的下一个问题是 - 我不知道该怎么做。如果我在我的项目表单中添加一个选择行,以添加 user_ids,其中 user.organisation 等于当前用户的 id,那么创建项目的学生应该能够看到可能的选项列表。
然后在我的项目展示页面中,我想展示团队中的每个学生。
谁能帮助解决这个问题?我迷路了,不知道在哪里可以找到类似问题的示例。
更新
虽然我很困惑。我不知道我是否应该与用户一起加入项目(通过我称为团队的加入表),或者我是否应该通过名为“团队”的加入表加入用户和用户。
如果我将用户加入项目,那么创建项目的用户可以选择其他用户作为项目团队成员对我来说是有意义的。然而,并不是说每个项目都有很多团队(这就是这个例子所展示的)。我不确定是否将 has_many 更改为 has_one,因为本文继续通过 join 解释 has_many。
如果我将用户加入用户,那么拥有许多项目的用户可能会为每个项目拥有不同的团队。所以这是不正确的。
以文章为例,我试过了:
创建团队模型:
class CreateTeams < ActiveRecord::Migration
def change
create_table :teams do |t|
t.references :project, index: true, foreign_key: true
t.references :team_mate, index: true
t.timestamps null: false
end
add_foreign_key :teams, :projects, column: :team_mate_id
add_index :teams, [:project_id, :team_mate_id], unique: true
end
end
团队.rb
belongs_to :project
belongs_to :team_mate, class_name: "Profile"
团队控制器(我稍后会解决这个问题 - 因为我还没有媒人部分,所以现在对其进行评论):
class TeamsController < ApplicationController
before_action :resync_matches, only: :index
def index
# several orders of magnitude faster
@team_mates = current_user.team_mates
.page(params[:page])
end
private
def resync_matches
# only resync if we have to
if current_user.teams_outdated?
new_matches = MatchMaker.matches_for(current_user)
current_user.team_mates.replace(new_matches)
end
end
end
项目.rb
has_one :team
has_many :team_mates, through: :teams, dependent: :destroy
我对此进行了更改,以便项目拥有一个团队而不是多个团队。
我对此感到困惑,不知道如何启动和运行它。在我的项目表单中,我想让用户(创建项目的人)选择其他用户的个人资料,这些用户是队友。我现在迷路了。
我尝试制作团队助手:
module TeamsHelper
def team_mate_options
s = ''
Profile.in_same_organisation.each do |profile|
s << "<option value='#{profile.id}'>#{profile.user.full_name}</option>"
end
s.html_safe
end
end
在我的 profile.rb 中,我尝试创建一个范围来获取与项目创建者属于同一组织的个人资料(尽管我不确定这是否正确):
scope :in_same_organisation, -> (organisation_id) { where(organisation_id: organisation_id) }
然后在我的项目表单中,我尝试添加一个选择选项:
<div class="form-group">
<%= label_tag 'team_mates', 'Choose team mates' %>
<%= select_tag 'team_mates', team_mate_options, multiple: true, class: 'form-control chosen-it' %>
</div>
维沙尔的建议
根据 Vishal 的建议,我已经实施了建议的结构。我的项目表格有问题。我的完整设置是:
模型 组织
has_many :profiles
简介
has_many :projects
belongs_to :organisation
has_many :teams, foreign_key: "team_mate_id"
has_many :team_projects, through: :teams, source: :project
项目
belongs_to :profile
has_many :teams
has_many :team_mates, through: :teams
团队
belongs_to :project
belongs_to :team_mate, class_name: "Profile"
我的团队表有:
create_table "teams", force: :cascade do |t|
t.integer "project_id"
t.integer "team_mate_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
然后在我的项目表单中,我有:
<%= f.label :team_mates, :label => "Add a team member" %>
<%= f.collection_select(:team_mate_id, Profile.all, :id, :team_mate_select, {prompt: "Select the team member"}, {:required => true}) %>
在我的个人资料模型中,我有:
def team_mate_select
self.user.formal_name
end
我的结构是个人资料属于用户。在用户中,我有一个名为正式名称的方法,它为用户名添加了一个标题。
当我保存并尝试时,我收到一条错误消息:
undefined method `team_mate_id' for #<Project:0x007fa08ed3d8e0>
(突出项目表单的集合选择行)
我的项目/form.html.erb 有:
<%= simple_form_for(@project) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :title, :label => "Title", autofocus: true %>
<%= f.input :description, :as => :text, :label => "Describe your project", :input_html => {:rows => 10} %>
<%= f.input :remark, :as => :text, :label => "Is there an interesting fact or statistic that's relevant to this research?", :input_html => {:rows => 5}, :placeholder => "In fact, ...(insert a fact which shows why this research might be interesting or relevant)" %>
<%= f.input :hero_image, :label => "Add an image" %>
<%= f.label :team_mates, :label => "Add a team member" %>
<%= f.collection_select(:team_id, Profile.all, :id, :team_mate_select, {prompt: "Select the team member"}, {:required => true}) %>
<div class="form-actions" style="margin-top:50px">
<%= f.button :submit, "Create", :class => 'formsubmit' %>
</div>
<% end %>
【问题讨论】:
标签: ruby-on-rails arrays select associations